[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details

Interating all fields in a form

Asked by stefanlennerbrant in JavaScript, Dynamic HTML (DHTML)

Tags: Iterate, form fields, compatibility, Internet Explorer, Firefox

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?
[+][-]08/28/08 07:27 AM, ID: 22335239Expert Comment

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 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08/28/08 07:28 AM, ID: 22335253Expert Comment

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 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08/29/08 05:01 AM, ID: 22344468Author Comment

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 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08/29/08 05:12 AM, ID: 22344553Author Comment

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 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08/29/08 07:01 PM, ID: 22350252Expert Comment

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 30-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-92 / EE_QW_EXPERT_20070906