简体   繁体   中英

select all input value in input type number

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.

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();
});

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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM