determine if any of checkbox[] is checked

1,240 views
Skip to first unread message

Phil Petree

unread,
Mar 31, 2013, 11:30:57 AM3/31/13
to prototype-s...@googlegroups.com
Happy Easter!
 
I have an array of checkboxes on a form (about 15 of them) arranged in rows like this:
<tr><td><input type='checkbox' name='kitcat[]' ...></td><td>name</td><td>description</td></tr>
 
What I have been trying to use is this:
$("input:checkbox[name=kitcat]:checked").each(function() {
    // if one is checked, we validated
    return true;
});
 
What I'm getting is an error saying: Unable to get value of the property 'each': object is null or undefined
 
What's the trick to this?

Jason Westbrook

unread,
Mar 31, 2013, 11:54:57 AM3/31/13
to prototype-s...@googlegroups.com
Try adding a separate class to all of your checkboxes that are in the group and then do this

if( $$('.groupchecks:checked').length == 0 )
{
    //no checked checkboxes

{

Jason Westbrook | T: 313-799-3770 | jwest...@gmail.com


--
You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group.
To unsubscribe from this group and stop receiving emails from it, send an email to prototype-scripta...@googlegroups.com.
To post to this group, send email to prototype-s...@googlegroups.com.
Visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

mike

unread,
Mar 31, 2013, 12:50:19 PM3/31/13
to prototype-s...@googlegroups.com
try
$$("...")
because $() equal document.getElementById

Phil Petree

unread,
Mar 31, 2013, 2:46:01 PM3/31/13
to prototype-s...@googlegroups.com
Hey Jason... I guess the rest of the group are all slugs! How dare they take Easer off! LOL
 
I had kinda come to the same conclusion... what I implemented was:
      if($$('input:checked').length > 0)
        return true;
This kind of works because the ONLY input on this page are checkboxes (think of a wizard) but I was really looking to pass names (kitcat[]) in so that it could be more flexible and accomodate future mods.
 
In your method, I need to mod all 17 checkboxes to include the same class name which is currently: "validate-onein-group"
 
Is there a way to collect all checkboxes with the same name?

Jason Westbrook

unread,
Apr 1, 2013, 1:15:24 PM4/1/13
to prototype-s...@googlegroups.com
typically when I build forms like that I have a database that is giving me all the data so I only have to update the loop once for all the checkboxes :-)

but yes you can use the $$() Selector method to use the names - I think you have to do it this way though

$$("input[name='kitcat[]']:checked")    //but I'm not sure how Sizzle will handle the bracket in the name
or
$("input[name^='kitcat']:checked")     //this is probably more safe but it will match more elements as it gets all elements that have a name attribute that starts with "kitcat" (starts with is ^= instead of =)



Jason Westbrook | T: 313-799-3770 | jwest...@gmail.com


Phil Petree

unread,
Apr 1, 2013, 1:49:43 PM4/1/13
to prototype-s...@googlegroups.com
// this works with names that contain spaces and arrays[]...
 
Validation.add('validate-one-array', 'Error message text', function(value, element) {
    var elmName = "input:checkbox[name='" +element.name +"']";
    var bChecked = false;  // not validated
    $$(elmName).each(function(elm) {
       if( $(elm).checked )
         bChecked = true;     // if one is checked, we validated
    });
    return bChecked;
  });


Jason Westbrook

unread,
Apr 1, 2013, 3:53:06 PM4/1/13
to prototype-s...@googlegroups.com

Prototype has a method for what you are trying called any()


you create an iterator function and pass it to any


    var elmName = "input:checkbox[name='" +element.name +"']";
    var bChecked = false;  // not validated
    var anychecked = $$(elmName).any(function(n) {
         return n.checked;
    });

    anychecked will be true or false



Jason Westbrook | T: 313-799-3770 | jwest...@gmail.com


Phil Petree

unread,
Apr 1, 2013, 8:11:20 PM4/1/13
to prototype-s...@googlegroups.com
Ha!  I knew there had to be a shorter way to do this!
 
Thanks!

Reply all
Reply to author
Forward
0 new messages