this.form.elements.length results in 0

D

dan

I am a bit of a java newbie so please forgive me.

I am trying to do a simple check-all / un-check-all checkbox system.
All of the solutions I have seen use "this.form.elements.length" or a
variant of this.

My scripts don't work because "this.form.elements.length" always
returns 0. Example:

document.write(this.FormName.elements.length);

displays "0"

Am I missing something obvious here?

Thanks,

Dan T
 
E

Evertjan.

wrote on 01 apr 2006 in comp.lang.javascript:
I am a bit of a java newbie so please forgive me.

You certainly are, Java being a language that is OT here.

Javascript is NOT a script version of Java, but quite another language.
I am trying to do a simple check-all / un-check-all checkbox system.
All of the solutions I have seen use "this.form.elements.length" or a
variant of this.

My scripts don't work because "this.form.elements.length" always
returns 0. Example:

document.write(this.FormName.elements.length);

displays "0"

Am I missing something obvious here?

Yes:

Firstly, "this" only can be used AFTER the DOM has constructed the form,
so can only be called from an onclick or onmouseover etc.
document.write() then will destroy the whole page INCLUDING the script on
page.

Secondly, try:

<form name=f>
<input name=c type=checkbox>
<input name=c type=checkbox>
<input name=c type=checkbox>
<input name=c type=checkbox>
</form>

number of checkboxes:
<script type='text/javascript'>
document.write(document.forms['f'].elements['c'].length)
</script>
 
E

Evertjan.

Evertjan. wrote on 01 apr 2006 in comp.lang.javascript:
wrote on 01 apr 2006 in comp.lang.javascript:

You certainly are, Java being a language that is OT here.

Javascript is NOT a script version of Java, but quite another language.

And you are forgiven....
 
D

Dr John Stockton

JRS: In article <[email protected]>
, dated Fri, 31 Mar 2006 23:37:25 remote, seen in
news:comp.lang.javascript, (e-mail address removed) posted :
I am a bit of a java newbie so please forgive me.
^^^^

To avoid making foolish mistakes like that again, you should seek and
read the newsgroup FAQ before posting.
 

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,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top