Search code examples
javascriptreactjshtml-input

cant add setSelectionRange to selected element


I've selected input using "getElementById". After I want add "setSelectionRange" method to that element. But there is an error "property 'setselectionrange' does not exist on type 'htmlelement' " . What could be the problem?

const textfield = document.getElementById("masktextfield")
console.log(textfield)

that's what shows in console

enter image description here

now adding "setselectionrange"

textfield.setSelectionRange(0,0) // property 'setselectionrange' does not exist on type 'htmlelement'

Solution

  • const textfield = document.getElementById("masktextfield");
    textfield.focus();
    textfield.setSelectionRange(2, 5);
    <input type="text" id="masktextfield" value="sathish"/>