[thelist] DOM question

Peter-Paul Koch gassinaumasis at hotmail.com
Fri Dec 6 11:01:00 CST 2002


>can someone tell me how I would rewrite this reference to a hidden field
>value to work with the getElementById method?
>	document.formName.elementName[randy].value
>where [randy] is a function.  Thats the bit I have had trouble with.

Don't rewrite it. formName.elementName is the Level 0 DOM, which means that
it'll work in all JavaScript browsers. getElementById, on the other hand,
only works in the modern browsers. So formName.elementName is the better
choice.

Besides, if you want a random element of the form, you should do

document.formName.elements[randy].value

since 'elementName' is the name of one specific element, and unless the
element is a set of radio buttons it's not an array (unless you use a lot of
form fields with name 'elementName'.

>This:
>	document.getElementById(idName)[random].value
>returns an error of "idName has no properties" (javascript console NS7)

There is (should be) only one element with a specific ID. getElementById
doesn't return an array, like getElementsByTagName() does. Instead it
returns the single element with the correct ID.

>here is a snippet of the actual code:
>
>	var randy = Math.round(Math.random() * max)

Has this variable been initialized before you use the document.formName.etc?

--------------------------------------------------
ppk, freelance web developer
Interaction, copywriting, JavaScript, integration
http://www.xs4all.nl/~ppk/
Column "Keep it Simple": http://www.digital-web.com/columns/keepitsimple/
What shall we do with the W3C DOM?:
http://www.evolt.org/article/What_shall_we_do_with_the_W3C_DOM/4090/47311/index.html
--------------------------------------------------


_________________________________________________________________
Add photos to your messages with MSN 8. Get 2 months FREE*.
http://join.msn.com/?page=features/featuredemail




More information about the thelist mailing list