Advertisement

08.28.2008 at 05:41AM PDT, ID: 23685175 | Points: 500
[x]
Attachment Details

Interating all fields in a form

Asked by stefanlennerbrant in JavaScript, Dynamic HTML (DHTML)

Tags: , , , ,

I'm using the following function to iterate through fields in a form.
However, I need to make sure it works both in IE and FireFox (and in other browsers)

I have succesfully used:

 var elements = document.myForm.elements;
 var length = elements.length;
 for(var loop = 0; loop < length; loop++)
  if(elements.item(loop).name=="test") alert('Test found');

However, it takes very very long time if there are many many fields in the form. I've identified that elements.item() is to blame

Now, I've changed it to
 var elements = document.myForm.elements;
 var length = elements.length;
 for(var loop = 0; loop < length; loop++)
  if(elements[loop].name=="test") alert('Test found');

This works much faster, but I've understood that it is not really the proper way to do things, as the use of elements[] implicitly uses the "all" collection that only works in Internet Explorer?
Or is this "only illegal" when doing it this way instead (I haven't really tried on FireFox yet):
 var length = document.myForm.elements.length;
 for(var loop = 0; loop < length; loop++)
  if(document.myForm.elements[loop].name=="test") alert('Test found');

So, the question in:
which is the proper (compatible) way of iterating through fields in a form. And still not pay the performance penalty that item() seems suffer from.


Another way of doing it, which makes me pussled, is the following:
 var elements = document.myForm.elements;
 for(var value in elements)
  if(value=="test") alert('Test found');

It works, but it iterates through a lot more than just the fields!
Apart from the field names themselves, values like "language, scrollHeight, isTextEdit, currentStyle" etc etc is looped through.
The "length" attribute of elements is however the same, such that the actual content of elements (seen by "for value in elements") is larger than the "length" attribute.
Why is this?
Start Free Trial
 
Loading Advertisement...
 
[+][-]08.28.2008 at 07:27AM PDT, ID: 22335239

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08.28.2008 at 07:28AM PDT, ID: 22335253

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08.29.2008 at 05:01AM PDT, ID: 22344468

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08.29.2008 at 05:12AM PDT, ID: 22344553

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08.29.2008 at 07:01PM PDT, ID: 22350252

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_EXPERT_20070906