Search code examples
javascriptiosinputkeyboard

Selecting iOS-Keyboard when input is created by javascript


When creating an input by javascript, mobile safari does not choose the right keyboard. For first input the decimalPad-keyboard is shown, but for second input only the numbersAndPunctuation-keyboard is shown.

How can I fix this?

let myInput = document.createElement('input');
myInput.id = 'second';
myInput.type = 'number';
myInput.min = '0';
myInput.step = 'any';
myInput.inputmode = 'decimal';

document.getElementById('here').appendChild(myInput);
first input: <input id='first' type='number' min='0' step='any' inputmode='decimal'>

<div id='here'>second input: </div>


Solution

  • Instead of using :

    myInput.inputmode = 'decimal';
    

    use this:

    myInput.setAttribute('inputmode','decimal');
    

    OR

    myInput.inputMode = 'decimal'; // uppercase M
    

    Explanation: In general the "DOM attributes" for html nodes are usually written in camelCase if you use javascript! Inside the HTML you will use the "alllowercase" version. "DON'T ASK WHY. ;-)". But it is more recommended to use setAttribute() method on nodes because this will always end up in an DOM element attribute no matter what ever you writin'