Hyper Text Markup Language (HTML) Question: Download HTML PDF

How do I set the focus to the first form field?

Tweet Share WhatsApp

Answer:

You cannot do this with HTML. However, you can include a script after the form that sets the focus to the appropriate field, like this:

<form id="myform" name="myform" action=...>
<input type="text" id="myinput" name="myinput" ...>
</form>

<script type="text/javascript">
document.myform.myinput.focus();
</script>

A similar approach uses <body onload=...> to set the focus, but some browsers seem to process the ONLOAD event before the entire document (i.e., the part with the form) has been loaded.

Download HTML PDF Read All 84 HTML Questions
Previous QuestionNext Question
How can I use tables to structure forms?How can I use forms for pull-down navigation menus?