[Javascript] Reset the value of a SET of fields

Chris T christ at saeweb.com
Thu May 20 08:08:00 CDT 2004


> > You should use the EVAL on javascript
> > such as
> >
> > var tempObj;
> > tempObj=EVAL("document.form_name.first_name_" + num );
> >
>
> You should avoid eval at all costs - it's a terribly inefficient way of
> doing things. The same effect can be achieved using:
>
> var tempObj = document.form_name["first_name_" + num];


I agree about Eval. I cringe when I see it.

Collections exist for a reason and I firmly believe they should be used in
any situation over the EVAL statement. Not neccessarily for efficiency
because let's be honest - most scripts won't be a problem for today's
computers and since it's client-side, you won't have to perform this many
times causing a heavy server load. It's just more elegant with collections.

Now to answer your question, if you know the names of your fields
beforehand, I would just create an array, like such:

var arrGroup = new Array("field_name_1", "field_name_2", "field_name_3")
var objForm = document.forms["FormName"]
for(var = x; x < arrGroup.length; x++){
    objForm.elements[x].value = ""
}

Need to add items to the group?  Add an element to the array. Removing
items? Just as easy.

Now if you don't know the names of your fields in advance, you would
probably be relagated to adding a custom attribute to each of them - perhaps
something like Group="1" for all the Group1 items.

Then you loop through all your form elements checking if that attribute
matches your value.

Chris Tifer
http://emailajoke.com




More information about the Javascript mailing list