views:

91

answers:

3

I have an input text box with some text in it , onclick event I want to run a javascript function to select (highlight) all text that is in this box , how I can do that with jquery?

+1  A: 

You can do this:

$('input').click(function() {
 // the select() function on the DOM element will do what you want
 this.select();
});

Of course, then you can't click on the element to select an arbitrary point in the input, so it might be better to fire that event on focus instead of click

Quick Demo w/ click or w/ focus

gnarf
Is that gnarf like Pinky used to say on pinky and the brain?
Zoidberg
@Zoidberg - Nope, that would be `narf` --- Although pronounced the same, `gnarf` is simply my last name backwards.
gnarf
+5  A: 

You may take a look at this article. (first result on google)

Darin Dimitrov
+3  A: 

No need for jQuery, this is simple with the DOM and works in all mainstream browsers:

input.onfocus = function() { this.select(); };

If you must do it with jQuery, there's very little difference:

$(input).focus(function() { this.select(); });
Tim Down
Not sure if this will work in IE 6... but who cares!! Hehehehe
Zoidberg
It will. I'm pretty sure the non-jQuery version will work in IE 3. Certainly IE 4.
Tim Down