Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to select all input text on iphone device when focus / click?

Is it possible to select all input text on iphone device when focus / click?

Typically for a browser environment one would use:

$('input').bind("click focus", function() {
$(this).select();
});

This works for desktop browsers but appears unresponsive for iphone and presumably other mobile devices.

like image 653
Michel Joanisse Avatar asked May 04 '12 16:05

Michel Joanisse


3 Answers

Basically you have to REARRANGE the events focus, then click, then touchstart

$('#myID').on('focus click touchstart', function(e){
$(this).get(0).setSelectionRange(0,9999);
//$(this).css("color", "blue"); FUBAR
e.preventDefault();
});
like image 195
adnan tariq Avatar answered Oct 22 '22 02:10

adnan tariq


Setting a (small) timeout works best for me: The iOS keyboard seems to be called in a delegate or something, as it kept loosing my focus event to be able to select stuff. The select() did not work for the same reason.

I fixed it this way:

function focusMe(vitem) {
    window.setTimeout(function() { 
      vitem.setSelectionRange(0, 9999);
    }, 10);
}

<input onfocus="focusMe(this)" />
like image 42
Oscar Bout Avatar answered Oct 22 '22 00:10

Oscar Bout


$('input[type="text"]').on('focus',function(){
    $(this).get(0).selectionStart=0;
    $(this).get(0).selectionEnd=999;
})

I originally found this information here, and there are other things people have tried that have achieved the result.

like image 37
dgo Avatar answered Oct 22 '22 00:10

dgo