hamish hamish - 3 months ago 8
HTML Question

How to keep the insertion point in a text input when button is clicked

I am trying to make a program and I need the insertion point to go back to the text input when a button is clicked. Any ideas, thanks.



<input type="text" name="fname" id="name">
<p>
<button id="go">Go</button>




Answer

You can do it with JavaScript by attaching an event listener to the button. Fiddle.

<script>
var button = document.getElementById("go");
button.addEventListener("click", function(){
    document.getElementById("name").focus();
});
</script>