I have two input tags with a numeric identifier in the form on the same html page. In focus, I would like to set the value of the second input of the first and second characters of the first input. Then I want to deselect the text in the second input. Is there a way to do this? I wrote this HTML code:
<label>Num. Fattura: <input type="text" name="numfattura1" onkeypress="return event.keyCode != 13" id="1" /> </label> <label>Importo: <input type="text" name="importo1" onkeypress="return event.keyCode != 13" /> </label> <br /> <label>Num. Fattura: <input type="text" name="numfattura2" onkeypress="return event.keyCode != 13" id="2" onfocus="twoDigits(2)" /> </label> <label>Importo: <input type="text" name="importo2" onkeypress="return event.keyCode != 13" /> </label> <br />
And this javascript function:
function twoDigits(id){ var previousId = id - 1; var prevoiusValue = document.getElementById(previousId).value; document.getElementById(id).value = prevoiusValue.substring(0,2); document.selection.empty(); window.getSelection().removeAllRanges() }
I cannot find how to deselect the text that will be selected automatically.
source share