Maybe you are calling JavaScript before the input element is displayed? Place an input element in front of JavaScript, or wait for the page to load before your JavaScript starts.
In this order, it works fine:
<input type="text" id="test" />
<script type="text/javascript">
document.getElementById("test").focus();
</script>
In jQuery, you can put your code in .ready()to execute your code first when the DOM is fully loaded:
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#test").focus();
});
</script>
source
share