View Full Version : xhtml doctype and form name attribute in javascript?

08-21-2010, 05:08 AM
Hi, I have been attempting to transition to use of xhtml strict doctype and
my text editor, BBEdit (on Mac) tells me, when I ask it to check syntax,
that the attribute 'name' is not allowed in form object, as in any other
form element that I tried to use it in. This begs the question, how do
I script forms with javascript in the context of this doctype? None of
the javascript texts I have address this issue (O'Reilly Rhino book and

I could figure it out, but it appears to be a complicated process, just
doing getElementById() and sorting it out.
Does anyone have a reference to material that deals with this issue?
Or do I just abandon xhtml strict doctypes?

Thanks for time and attention

08-22-2010, 03:02 AM
Well, I dropped xhtml strict doctype and am using transitional,
so the document validates when using name attribute in form tag
and elsewhere.

Old Pedant
08-22-2010, 05:34 AM
It's kind of a weird change, but indeed the proper thing to do is stop using names for <form>s and use ids instead.

For people who were used to coding like this:

function validate( )
if ( document.form1.name.value == "" )....
if ( document.form1.address.value == "" ) ...

then, yeah, it's ugly. You don't want to have to code

function validate( )
if ( document.getElementById("form1").name.value == "" )....
if ( document.getElementById("form1").address.value == "" ) ...

But that always was a dumb way to code such things.

I have always used something like:

function validate( )
var form = document.form1;
if ( form.name.value == "" )....
if ( form.address.value == "" ) ...

so for me it is trivial to switch over to

var form = document.getElementById("form1");

Even more, whenever possible I don't give *EITHER* a name or id to my forms.

After all, if you code:

function validate( form ) ...

And then invoke it via

<form ... onsubmit="validate(this);">

or maybe

<select name="whatever" onchange="if ( validate(this.form) ) this.form.submit();" >

and so on, there's really no need for either name or id.

Finally, if you only have one <form> on your page, which most of us do, you can always use

var form = document.forms[0];

So I, for one, don't see that using forms without names is any change at all.