Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Closed Thread
Checkbox array problem
Old 09-11-2009, 07:16 PM Checkbox array problem
Junior Talker

Posts: 3
Location: Lancaster, PA
Trades: 0
Hello,

I ran into a problem just now, counting the number of checkboxes in a group where they all share the same name. When I have two (or more) checkboxes, the length property appears as two (or as the expected value). But when I have just one checkbox, the length appears as "undefined"! For example

HTML Code:
<html>
<body>
<form name="theform">

<table>
<tr>
<td><input type="checkbox" name="contact_list" value="Email: john.doe@foo.com" /></td>
<td>Email</td>
<td>john.doe@foo.com</td>
</tr>
<tr>
<td><input type="checkbox" name="contact_list" value="Email: jane.doe@foo.com" /></td>
<td>Email</td>
<td>jane.doe@foo.com</td>
</tr>
</table>

<p>
<input type="button" onclick="alert(document.theform.contact_list.length);" value="Click Me" />

</form>
</body>
</html>
This works until you remove one of the table rows. But I've tested this in Firefox 3.5, IE7, and Safari, and all have the same problem, which tells me that I've made some mistake. Does anyone have suggestions?

Thanks!
thetao2 is offline
View Public Profile
 
 
Register now for full access!
Old 09-11-2009, 08:22 PM Re: Checkbox array problem
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
I always use the DOM functions, so not sure what's up with your actual issue, but here's a functioning solution:
HTML Code:
<html>
<body>
<form name="theform">

<table id="thetable">
<tr>
<td><input type="checkbox" name="contact_list" value="Email: john.doe@foo.com" /></td>
<td>Email</td>
<td>john.doe@foo.com</td>
</tr>
<tr>
<td><input type="checkbox" name="contact_list" value="Email: jane.doe@foo.com" /></td>
<td>Email</td>
<td>jane.doe@foo.com</td>
</tr>
</table>

<p>
<input type="button" onclick="alert(document.getElementById('thetable').getElementsByTagName('INPUT').length);" value="Click Me" />

</form>
</body>
</html>
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
View Public Profile Visit JeremyMiller's homepage!
 
Old 09-11-2009, 08:52 PM Re: Checkbox array problem
Junior Talker

Posts: 3
Location: Lancaster, PA
Trades: 0
Thanks for the fast reply. Your suggestion is more elegant than what I was going to try, and also removes the need to use try/catch when there are no checkboxes present on the page. It works for me!
thetao2 is offline
View Public Profile
 
Closed Thread     « Reply to Checkbox array problem
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.11523 seconds with 12 queries