Put the cursor back of the value instead of front of the value

<script type="text/javascript">
window.onload = function() {
  var input = document.getElementById("focusBox").focus();
}
</script>

<input type="text" id="focusBox" value="myValue">

The code above put the cursor in front of “m” in the word myValue".

I like to put the cursor back of “e” in the word "myValue

Hi @joon1, you can set the selectionStart / selectionEnd properties of the element then:

<input type="text" id="focusBox" value="myValue">

<script type="text/javascript">
  var input = document.getElementById("focusBox")
  
  input.focus();
  input.selectionStart = input.selectionEnd = input.value.length
</script>

(BTW note that currently you’re just assigning undefined to the input variable as the focus() method doesn’t return anything.)

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.