An element named 'length'

D

D. Alvarado

Hello, I'm having problems on IE 6.0 for Win XP. I want to iterate
through the elements of a form, but one of those elements is named
'length'. So, this JS code doesn't display any alert boxes, even
though it should display 3.

for (var i=0; i<myForm.elements.length; i++) {
alert(myForm.elements.name);
}

Can anyone suggest a cross-browser work-around? Thanks -
 
B

Brian Genisio

D. Alvarado said:
Hello, I'm having problems on IE 6.0 for Win XP. I want to iterate
through the elements of a form, but one of those elements is named
'length'. So, this JS code doesn't display any alert boxes, even
though it should display 3.

for (var i=0; i<myForm.elements.length; i++) {
alert(myForm.elements.name);
}

Can anyone suggest a cross-browser work-around? Thanks -


Hmmmm...

Option #1:
Rename the offending element to something else (such as len)

Opeiont #2:

for(var i in myForm.elements)
{
alert(myForm.elements.name);
}

Brian
 
M

Michael Winter

Hello, I'm having problems on IE 6.0 for Win XP. I want to iterate
through the elements of a form, but one of those elements is named
'length'. So, this JS code doesn't display any alert boxes, even
though it should display 3.

for (var i=0; i<myForm.elements.length; i++) {
alert(myForm.elements.name);
}

Can anyone suggest a cross-browser work-around? Thanks -


Use FormObject.length instead.

In future, don't use names that might conflict with object properties and
methods.

Mike
 
R

Richard Cornford

Use FormObject.length instead.

That probably won't help as providing the shortcut notation of allowing
named form controls to be accessed as named members of the form object
will mean that the - length - property of the form will have been
replaced with a reference to the same form control.

On DOM browsers:-

var inputs = formObject.getElementsByTagName('input');
var selects = formObject.getElementsByTagName('select');

- and iterating over the resulting nodeList objects. But there has got
to be a better way.
In future, don't use names that might conflict with
object properties and methods.

And that is it. :)

Richard.
 
M

Michael Winter

That probably won't help as providing the shortcut notation of allowing
named form controls to be accessed as named members of the form object
will mean that the - length - property of the form will have been
replaced with a reference to the same form control.

I seem to be overlooking a lot of details today...

Thank you. I'd also like to thank you for your advice on DOM feature
detection given last week (23rd).

Mike
 
R

Richard Cornford

I seem to be overlooking a lot of details today...

So am I. I forgot about textarea elements.
... . I'd also like to thank you for your advice on
DOM feature detection ...

You are welcome. It is probably one of the most important aspects of
matching a script with a browser's possible support for it and deserves
talking about from time to time.

Richard.
 
M

Michael Winter

So am I. I forgot about textarea elements.

I actually noticed that. :) For thoroughness, you could also mention
BUTTON elements, but they wouldn't be accessed nearly as much.

Mike
 
J

John

(e-mail address removed) (D. Alvarado) wrote in
Hello, I'm having problems on IE 6.0 for Win XP. I want to iterate
through the elements of a form, but one of those elements is named
'length'. So, this JS code doesn't display any alert boxes, even
though it should display 3.

for (var i=0; i<myForm.elements.length; i++) {
alert(myForm.elements.name);
}

Can anyone suggest a cross-browser work-around? Thanks -


Umm, am I missing something? Why not just change the name of the element
named 'length'?

John
 
D

D. Alvarado

John said:
(e-mail address removed) (D. Alvarado) wrote in

Umm, am I missing something? Why not just change the name of the element
named 'length'?

John

Normally a good idea. I do not have control over the variable name --
it is required by a third party site to which I'm passing data.

Thanks to all for the helpful answers. -
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top