Home > other >  how to put input box cursor at end of text
how to put input box cursor at end of text

Time:01-09

I want to add a number in input field on button click and set the cursor position at the end of text. this should happen every time when the button is clicked.

<input id="input"></input>
<button onclick="addnumber()">add</button>
addnumber(){

}

CodePudding user response:

<input id="input"></input>
<button onclick="addnumber()">add</button>
    
<script>
    function addnumber() {
        const myInput = document.getElementById('input');
        const textLength = myInput.value.length;

        myInput.focus();
        myInput.setSelectionRange(textLength, textLength);
    }
</script>

CodePudding user response:

let input = document.getElementById('input')
function addnumber() {
  input.value=69
  input.focus()
}
<input id="input">
<button onclick="addnumber()">add</button>

CodePudding user response:

JS code :

let input = document.getElementById('input')
function addnumber() {
  input.focus(); 
  var val = this.input.value; 
  input.value = ''; 
  input.value = val;
}

HTML code:

  <input id="input">
  <button onclick="addnumber()">add</button>
  •  Tags:  
  • Related