[thelist] Tip: Converting to XHTML, javascript, name, id

Frank framar at interlog.com
Sun Aug 25 06:42:01 CDT 2002


<tip type="Converting to XHTML, javascript, name, id" author="Frank Marion">

If you are converting your site or apps over to strict XHTML
1.0, you'll find that using the "name" attribute will raise
the validator's danders. You're doing simple error checking or
roll-overs via Javascript. So now you've got to use ID instead
of name. What do you do? What *do* you do?

Simple. Remember that every identified item in a document
must have a *unique* id. Remove the name, replace it with an
ID.

We change this:

   <input type="text"
          name="yourFieldName"
          value="Enter search keywords" />
to:

   <input type="text"
          id="yourFieldName"
          value="Enter search keywords" />

"You evil villain!", you exclaim, "This breaks my
javascript!" (Startled, Frank stops twisting his mustachio).
No problem. The adjustment to your script is quite minimal.

We change this:

   yourFormName.yourFieldName.value.length == 0

to:

   document.getElementById('yourFieldName')

That's it.

Some notes: You can make your code a little terser (shorter,
and most likely easier to read and type) by setting the field's
id to a variable, and simply referring to the variable's name
throughout your script instead of using the full declaration
all the way though.

Instead of:

   if (document.getElementById('yourFieldName').value
         == 'Enter search keywords')

You can do this:

 Declare your variable before you start

   var theField = document.getElementById('yourFieldName');

then use this:

   if (theField.value == 'Enter search keywords')

Hope this helps.

</tip>


--
Frank Marion,
framar at interlog.com




More information about the thelist mailing list