By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,686 Members | 1,914 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,686 IT Pros & Developers. It's quick & easy.

An element named 'length'

P: n/a
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[i].name);
}

Can anyone suggest a cross-browser work-around? Thanks -
Jul 20 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
On 2 Feb 2004 10:32:29 -0800, D. Alvarado <la***********@zipmail.com>
wrote:
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[i].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

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #2

P: n/a
D. Alvarado wrote:
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[i].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[i].name);
}

Brian

Jul 20 '05 #3

P: n/a
"Michael Winter" <M.******@blueyonder.co.invalid> wrote in message
news:op**************@news-text.blueyonder.co.uk...
<snip>
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.
Jul 20 '05 #4

P: n/a
On Mon, 2 Feb 2004 19:10:32 -0000, Richard Cornford
<Ri*****@litotes.demon.co.uk> wrote:
"Michael Winter" <M.******@blueyonder.co.invalid> wrote in message
news:op**************@news-text.blueyonder.co.uk...
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.


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

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #5

P: n/a
"Michael Winter" <M.******@blueyonder.co.invalid> wrote in message
news:op**************@news-text.blueyonder.co.uk...
<snip>
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.
Jul 20 '05 #6

P: n/a
On Mon, 2 Feb 2004 23:00:38 -0000, Richard Cornford
<Ri*****@litotes.demon.co.uk> wrote:
"Michael Winter" <M.******@blueyonder.co.invalid> wrote in message
news:op**************@news-text.blueyonder.co.uk...
I seem to be overlooking a lot of details today...


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

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #7

P: n/a
la***********@zipmail.com (D. Alvarado) wrote in
news:9f**************************@posting.google.c om:
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[i].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
Jul 20 '05 #8

P: n/a
John <jo**@mshome.net> wrote in message news:<Xn***************************@203.26.24.228> ...
la***********@zipmail.com (D. Alvarado) wrote in
news:9f**************************@posting.google.c om:

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. -
Jul 20 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.