Author Topic: Javascript Focus Form Field  (Read 4340 times)

Offline admin

  • Administrator
  • Sr. Member
  • *****
  • Posts: 296
    • View Profile
Javascript Focus Form Field
« on: January 06, 2010, 02:43:45 PM »
Code: [Select]
<form name=aaa>
This is GGG: <input type=text name=ggg><br>
This is HHH: <input type=text name=hhh>
</form>

<script type="text/javascript">

function dofo() {
document.aaa.hhh.focus();
}

</script>

<body onLoad=dofo()>

-We create an HTML form and name it aaa because that is easy to type and remember.
-We name our first text field ggg because we have a cat named G
-We name our second text field hhh because we want to.
-We write a little function and call it dofo because we think that is a clever name.
-document.aaa.hhh.focus();
---document - is the object we are manipulating, this page.
---aaa - is our form name.
---hhh - is the element name we want to place the focus on.
---focus() is a built-in Javascript method that places focus, the cursor in this instance.
-We call the function in our HTML body tag using onLoad.

When the page with this script loads, the cursor will be in the second text field, hhh.