Javascript Form How to - Move the cursor into a input text box by clicking on html button








Question

We would like to know how to move the cursor into a input text box by clicking on html button.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
window.onload=function(){<!-- w ww .  ja v a2 s  . c  o m-->
    document.getElementById('my_button').onclick = function() {
        document.getElementById('my_textbox').focus();
    };
}
</script>
</head>
<body>
  <input type="text" id="my_textbox" value="My Text" />
  <button id="my_button">Focus</button>
</body>
</html>

The code above is rendered as follows: