Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 356
  • Last Modified:

onLoad setting focus to a button or a link?...

I'm helping to build a data entry intensive application.  On a confirmation page the user will receive textual confirmation that the data they entered on the previous form has been submitted.  Underneath that are four options to choose from to continue in the data entry process (list, new, copy, edit).  When this page is loaded to have focus set on the first button or hyperlink (either way is fine) when the page loads.  Sometimes when the page loads and I hit 'Tab' it will drop me on the first link, but sometimes it doesn't and it's rather inconsistent.  I tried using the standard:

<body onload="document.form_name.input_name.focus()">

but it returns a javascript error even if I set the item as an image field in a form.  Is there an easy way to set focus on a button or hyperlink upon load?  The onLoad event of course works with a text field, but I haven't gotten it or similar to work with a button or hyperlink.  It doesn't even need to actually select the item.  It would be more than acceptable if the user was able to hit 'Tab' once and be on the first selection.  I need this to work in IE 4-6, Netscape 4-7.  Any suggestions?
  • 3
1 Solution


Fritz the Blank
Hi there.
You can't control pageelement on the "onload" event. Simply because the HTML parser reads the content from top down. Meening that if you put any elements in the onload the only element loaded at that time is the body!.

Instead you could try putting somthing like this at the bottom of your html file.
bla bla bla bla bla a kjh asdfnbdas
asdfh aksfhd kasjhdf
askhdfkajs hdf


Best regards from Denmark
I am not sure that is true. My understanding is that onLoad() executes as soon as the page is finished loading.

Fritz the Blank
swankboyAuthor Commented:
This worked perfectly.  xaungxaj gave me pretty much the same thing so I just accepted this one as it was first.  Also the onLoad event doesn't trigger until the page is completely loaded.  That's why when you go to start filling out a form and the page loads and all of a sudden you're typing in the wrong box because the event triggered the focus and you started typing before the page fully loaded.  Very annoying.  Thanks!  I wish I didn't have to use form images, but it works. Thanks!
Glad to have helped,

Fritz the Blank

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now