Select all input value in input type number

I have this on a webpage. When the user clicks on an input element, I want the value of the input elements to be automatically selected.

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

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

      

This will pick the value in firefox, chrome and IE. but it won't be on safari. i want to make it fit for iPhones too. hard google search I found a javascript method that safari supports that looks something like this.

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

      

But it does not support the "number" input type. So, is there any other way to achieve this?

As always, thanks for your time.

+3


source to share


2 answers


when you say safari, i assume you mean iOS safari? Anyway, on a touch device, the click event may not fire, but the touch event will fire instead. You can use them:



$("input[type='number']").on('click touchstart', function () {
    $(this).select();
});

      

-2


source


If you need to use document.getElementById()

, just provide your input ID!

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



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

-2


source







All Articles