Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

XHTML Strict javascript and form ID



7:24 am on Jan 28, 2003 (gmt 0)

10+ Year Member

Hello all,

I am rewriting a web site in XHTML 1.0 strict and I have run up against a problem with the fact that Name is no longer an attribute of Form. In earlier versions of HTML I could reference specific form fields with javascript...

For example: document.myform.myfield.focus() would put the focus in the field myfield in the form myform.

In XHTML Strict where Name is no longer allowed as form identifier this does not work. Does anyone know how I can work around this problem? Thanks.


7:51 am on Jan 28, 2003 (gmt 0)

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member

Welcome to Webmaster World!

All you have to do is give the individual fields an ID (the form doesn't need one) .. and refer to those ID's.

For example, consider the following code:

<input type="text" id="mytextfield" \>
<input type="password" id="blah" \>

To refer to the first field you use:


Likewise, to refer to the second field you use:


Simple, all you have to remember is the ID, and call it directly using getElementById


8:42 am on Jan 28, 2003 (gmt 0)

10+ Year Member

Thanks a lot, getElementById() does work :) However, I now have an additional problem. The input field where I want to put the focus is in a four-frame document. The frame that contains the input field loads first and I can see that it gets focus for a fraction of a second, but when the other frames finish loading the input field looses its focus again. Any ideas on how this can be solved?

My web site is generated dynamically so putting the getElementById() call in the last frame to load is not a practical solution since each frame are semi-independent from one another.



9:04 am on Jan 28, 2003 (gmt 0)

10+ Year Member

Okay...figured it out. Had forgot that I at some point had put some onload="self.focus()" calls in various pages. These were the ones that broke the input field focus. So, thanks again for the help...I sure do appreciate it!



Featured Threads

Hot Threads This Week

Hot Threads This Month