简体   繁体   中英

How to make value of <input type=“text”> selected with jQuery?

Suppose the jQuery object of the input is $input.

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

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?

Try this:

$('input, select, textarea').each(function() {
    $(this).focus(function() {
        if (this.select) {
            this.select();
        }
    });
});

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"); });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM