[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?

Joel,

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()
  for(i=0;i<len;i++)
  {
    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.

HTH

Tom

=====
http://www.pixelmech.com/
var me = tom.pixelmech.webDeveloper();

http://www.maccaws.com/
[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
http://platinum.yahoo.com


More information about the thelist mailing list