40

I was wondering if there was a way for text inside a input box (pre loaded using value="") to highlight when the user clicks on it?

input type='text' name='url' id='url' value='http://www.a-link.com/' />

EDIT

I need the text to he highlighted so the user can copy it.

Jay
  • 721
  • 3
  • 9
  • 15

8 Answers8

86
<input type="text" name="textbox" value="Test" onclick="this.select()" />
tskuzzy
  • 35,812
  • 14
  • 73
  • 140
  • 1
    1) If you bind it to `onfocus` instead of `onclick` then it will work for keyboard navigation as well as mouse clicks. 2) You can also access the input field from the emitted event object instead of from `this`. E.g. for react you can do ` e.target.select()} />` – Cam Jackson Oct 14 '21 at 10:02
  • Great answer, including the onfocus suggestion. I found a number of other suggestions, though all of them required JavaScript. – Cincy Steve May 22 '23 at 14:52
17

You could attach javascript to the click event to select the text like so:

$(document).ready( function() {
  $('#id').click( function( event_details ) {
    $(this).select();
  });
});

There is a potential issue where the user could be trying to click at a later point in the text to correct a typing mistake and end up selecting the whole thing. A better way would be to trigger this when the input gets focus from the user. you'd replace .click with .focus in the example above.

jQuery event documentation: http://api.jquery.com/category/events/

Andrew Kothmann
  • 607
  • 3
  • 4
14

Add the following onclick attribute to make the entire <input> automatically highlight when the user clicks on it:

<input type="text" value="Test1" onclick="this.select()" />

Alternatively, if you want the user to be able to change the selection after the initial click, change the onclick attribute to an onfocus attribute. This will also highlight the entire <input> when the user clicks on it, but it allows them to change the highlighted part manually afterwards:

<input type="text" value="Test2" onfocus="this.select()" />

Here is an example of both inputs in action.

WK_of_Angmar
  • 305
  • 1
  • 4
  • 8
3

You want to use focus property. Like this: http://jsfiddle.net/sCuNs/

html

  <p><input type="text" size="40"></p>

css

input:focus, textarea:focus{
background-color: green;
}
user194076
  • 8,787
  • 23
  • 94
  • 154
2
$('#foo').on('mouseup', function(e){
    e.preventDefault();
    $(this).select();
});

$('#foo').on('mouseup', function(e){
    e.preventDefault();
    $(this).select();
});
Johan
  • 18,814
  • 30
  • 70
  • 88
javadev28hu
  • 160
  • 1
  • 5
2

Do you mean to select the text?

Use onclick event to fire the code:

document.getElementById("target-input-id").select();
Sfisioza
  • 3,830
  • 6
  • 42
  • 57
0

This should do it:

<input type='text' name='url' id='url' onclick="this.select()" value='http://www.a-link.com/' />
tlaverdure
  • 522
  • 7
  • 18
-1
<input id="inputField" type="text" size="40" value="text to be highlighted"></p>
    
document.getElementById('inputField').focus();

The default behavior for focus selects the text in the input field. I was looking for a solution not to do that when I found this.

Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58
Bruce Lim
  • 745
  • 6
  • 22