tags:

views:

109

answers:

3

Suppose the jQuery object of the input is $input.

How to make its value selected(make it highlighted)?

+4  A: 

Let's say the id of the element is anInput, you should be able to do the following:

var $input = $("#anInput");
$input[0].select();

$input represents the wrapped set or jQuery object, as you put it. The zeroeth index of this set is the first DOM element matched by your selector (there is only one in this case). With that element in hand, the select function should select its contents.

Is this what you need?

David Andres
Use the select function to select/highlight the text, as shown in this post.
David Andres
Working now,t y
Shore
No prob...btw...you're right, you probably won't need the explicit call to focus here.
David Andres
Yes,I didn't use it:)
Shore
I don't think you'll need to get the zeroeth index either, I think when only one object is in the set jQuery maps requests to the set directly (so in this case $input) to the first, and only object (your input element).
Jamie Rumbelow
that's worth trying out, but in my experience jQuery will wrap even a single element in a set.
David Andres
A: 

Try this:

$('input, select, textarea').each(function() {
    $(this).focus(function() {
     if (this.select) {
      this.select();
     }
    });
});
TehOne
I think you understood my requirement.But your code is not working:(
Shore
this may only work when the element gets focus
David Andres
As dandres109 said, this block of code is used to select the text on focus. I thought that was what you were trying to accomplish.
TehOne
A: 

To focus the element you use

$(input).focus();

To highlight it you create an extra highligh CSS class that does the Highlighting you want (or you directly modify the element style) in the onfocus and blur events.

$(input).focus(function() { $(this).addClass("highlight"); });
$(input).blur(function() { $(this).removeClass("highligh"); });
Daff