10

i have this on a webpage. When a user click in the input element, i want the input elements value to be auto selected.

I can do this by making onclick event to this.select() like shown below

<input type="number" onclick="this.select();" >

This will selected the value in firefox, chrome and IE. but it won't in safari. an i wanna make this usable for Iphones too. tough google search i found a javascript method that safari support thats look something like this

document.getElementById(inputElementsId).setSelectionRange(0, 9999);

But it's not support for input type "number". So is there a other way to get this running?

As always, thanks for your time.

DaCh
  • 921
  • 1
  • 14
  • 48

2 Answers2

-2

when you say safari, i suppose you mean the iOS safari ? Anyway, on touch device, the click event might not be fired but the touch event instead. You can use these :

$("input[type='number']").on('click touchstart', function () {
    $(this).select();
});
Remy Grandin
  • 1,638
  • 1
  • 14
  • 34
  • 1
    This would throw an error... something like: The input element's type ('number') does not support selection – Tiago Reis May 11 '16 at 10:47
-3

If you have to use document.getElementById() then just give your input an ID!

<input id=num_input type="number" onclick="this.select();" >

Then you can select it with document.getElementById('num_input');

MikeTGW
  • 395
  • 2
  • 10