focus with selected text

3

I have an input on that I want it to have focus and the text is already selected.

Focus I'm doing this:

$(".autofocus").trigger('focus');

The problem is that the text is not selected. Does anyone know how to do this?

    
asked by anonymous 01.12.2016 / 18:18

2 answers

3

Place this code before the trigger code:

$(".autofocus").on("focus", function () {
   $(this).select();
});
    
01.12.2016 / 18:22
1

.on() is currently unnecessary, it would suffice ( as documentation ) before .trigger() the following code:

$(".autofocus").focus(function () {
    $(this).select();
});

You can still perform directly on the element:

<input class="autofocus" type="text" onfocus="this.select();" onmouseup="return false;" />
    
01.12.2016 / 18:34