[thelist] JavaScript array notation and similarly named form elements

Tom Dell'Aringa pixelmech at yahoo.com
Tue Apr 1 16:30:57 CST 2003

--- Joel Canfield <joel at spinhead.com> wrote:
> I have a group of text fields named WBE1, WBE2, . . . WBEn which,
> on some JavaScript event (onclick, onchange, whatever) I need to 
> clear. How can I loop thru all these similarly named elements 
> without knowing in advance how many there are, or how can I take a 
> count to use in a 'for' loop?


You can first run through the elements array and find how many form
fields you have. You can use "type" to find out how many text fields
are within those, then you can use substring() to pull the ones that
match some prefix (like WBE). Pass the form as a parameter, It would
be something like:

function clearText(theForm)
  len = theForm.length()
    if(theForm.elements[i].type == "text") //if its a text field
      if(theForm.elements[i].name.substring(0,3) == "WBE")
        theForm.elements[i].value = "";

This is untested off the top of my head but should get you going.



var me = tom.pixelmech.webDeveloper();

[Making A Commercial Case for Adopting Web Standards]

"That's not art, that's just annoying." -- Squidward

Do you Yahoo!?
Yahoo! Tax Center - File online, calculators, forms, and more

More information about the thelist mailing list