Here I’m explaining how to focus cursor to a text field on page load. You can do this with several methods with a simple javascript code. For this, you have to put a name for your form and your text filed. See the sample form below.

<form name="myForm" method="post" action="">

<label>

<input type="text" name="username" />

</label>

<label>

<input type="submit" name="Submit" value="Submit" />

</label>

</form>

Now just look on the code. You can do this with a single line code within body onLoad.

<body onLoad="document.myForm.username.focus();">

You can call this code with a function like this:

<script>

function curFocus(){

document.myForm.username.focus();

}

</script>

<body onLoad="curFocus();">

Another method is put the code inside the page after the form. See how is it.

<script type="text/javascript" language="JavaScript">

var focusCtrl = document.forms["myForm"].elements["username"];

if(focusCtrl) {

focusCtrl.focus();

}

</script>

Share with Friends

Related posts: