Search code examples
javascriptreactjsmasking

How to set mask textfield cursor at start position?


I'm using react mask textfield component . When I click on it , input cursor always shows up at the end of the mask . How to make when input is focused set that cursor after "+7(" ?

 <MaskedTextfield
    inputId="masktextfield"
    className="tf"
    reference={this.props.reference}
    maskedValue={this.props.maskedValue}
    numOfErrorMsgs={1}
    showMask
    guide
  />

enter image description here

enter image description here


Solution

  • You can use setSelectionRange(0,0);. First get the reference to the element, then apply that function. In plain JS the onFocus or onClick function would be like this:

    var textfield = document.getElementById('masktextfield');
    textfield.setSelectionRange(0,0);