views:

183

answers:

2

If I have groups of checkboxes like so:

<input type="checkbox" name"checkboxes1" value="#value1">
<input type="checkbox" name"checkboxes1" value="value2">
<input type="checkbox" name"checkboxes1" value="#value3">

<input type="checkbox" name"checkboxes2" value="value1">
<input type="checkbox" name"checkboxes2" value="#value2">
<input type="checkbox" name"checkboxes2" value="#value3">

Is there an easy way in jquery to return a value of 1 for each group of checkboxes, if both of the following conditions apply:

  • All of the checkboxes in the group that start with a # in their value are checked
  • All of the checkboxes in the group that don't start with a # in their value are not checked

Note: the names of the checkboxes are dynamically generated, so there could be any number in a group, any number of groups and each group could have any name.

Thanks :)

+1  A: 

You can select the attribute with [attr^=\#] pattern. So, you could code something like:

<html>
<head>
  <script src="./lib/jquery/jquery-1.3.2.js"></script>
  <script>
    var checkall = function() {

    var withHashAllChecked = true;
    var withoutHashAllChecked = true;

$("input.cb1:checkbox[value^=#]").each(function() {
    if (!this.checked) {
        withHashAllChecked = false;
    }
});


$("input.cb1:checkbox:not([value^=#])").each(function() {
    if (!this.checked) {
        withoutHashAllChecked = false;
    }
});

var result = withHashAllChecked && (!withoutHashAllChecked);
alert(result);

};

$(function() {
    $("input").click(checkall);
});
  </script>
</head>
<body>
<input class="cb1" type="checkbox" name"checkboxes1" value="#value1">#1</input>
<input class="cb1" type="checkbox" name"checkboxes1" value="value2">2</input>
<input class="cb1" type="checkbox" name"checkboxes1" value="#value3">#3</input>

<input class="cb2" type="checkbox" name"checkboxes2" value="value1">1</input>
<input class="cb2" type="checkbox" name"checkboxes2" value="#value2">#2</input>
<input class="cb2" type="checkbox" name"checkboxes2" value="#value3">#3</input>

</body>
</html>

EDIT

Updated Full working code. This is only checking checkboxes with class cb1, but just to give you an idea how to achieve. I must apologize that the original code has some problem that I didnt check it. Anyway, I try the above code and it should be fine (should you modify the jquery directory reference).

xandy
I can't seem to get it working with that code - always returns false. Could I set CONTEXT as the name of the current checkbox?
I've managed to get it working perfectly using your code and tweaking it a bit to use the name, not class - you did all the leg work though! :) thanks very much...
A: 
var checked = $('input:checkbox:checked[name=checkboxes1][value^=#]').length;
var notchecked = $('input:checkbox:not(:checked)[name=checkboxes1][value^=#]').length;
PetersenDidIt
I've managed to select the checkboxes with value that begin with # using your code above (but you don't need to include the input at the start), but how do i return 1 if the group of checkboxes with the same name are all checked if their value starts with #, and 0 if they are not? Thanks..
You just need to add to the selector a search for the name attribute with the value you want and then figure out if it found any results.
PetersenDidIt
would it be possible to insert the name, say, as an array? As i won't know the name of the checkboxes beforehand. So make a unique array for all checkbox names, that have at least one value that starts with #?Thanks :)