1

Id like to select all text from input on focusing:

<input id="selectMe" value="123" />
<input type="button" id="focusHim" value="Im working fine" />

JS:

$('#selectMe').focus( function() {
    $(this).select();
    console.log('focus fired');
});

// little test if focus event works fine
$('#focusHim').click( function() {
    $('#selectMe').focus();
});

This simplified code works fine, but sometimes there are some issues.

Here you have fiddle: http://jsfiddle.net/daxv2/

To see my problem, please click on input #selectMe, then click somewhere else and on #selectMe again. You will see, text is selected every even attempt. I don't know why.

P.S. When you change focus to click it works perfect:

$('#selectMe').click( function() {
        $(this).select();
        console.log('focus fired');
    });

So i suppose there is some problem with focus event.

Kasyx
  • 3,170
  • 21
  • 32

2 Answers2

1

It seems like the WebKit browsers interfere with this because of the mouseup event.

I added this and it seems to work fine:

$('#selectMe').mouseup(function () {
    return false;
});

http://jsfiddle.net/daxv2/6/

Pedro Estrada
  • 2,384
  • 2
  • 16
  • 24
1

You could use click event instead of focus. It works fine, also you can simplify your code by using this.select() instead of $(this).select() http://jsfiddle.net/daxv2/8/

m3div0
  • 1,556
  • 3
  • 17
  • 32