5
\$\begingroup\$

I need to select all checked checkboxes, except checkboxes having a class .checkall.

HTML:

<input type="checkbox" value="1961" class="checkbox">
<input type="checkbox" name="checkall" class="checkbox checkall">

Script:

$("input.checkbox:checkbox:checked:not(.checkall)").each(function () {
 var checkbox = $(this), machineId = checkbox.val()
}
Quill
12k5 gold badges41 silver badges93 bronze badges
asked Mar 6, 2013 at 7:32
\$\endgroup\$

2 Answers 2

7
\$\begingroup\$

I just posted on your other post about this, use:

$("input[type=checkbox]:checked:not(.checkall)")

This is the 'regular' way to check for input tags of a particular type, it's much clearer than your way. I'm not sure what the significance of the .checkbox class is but it seems like it's not necessary to my.

I also recommend you cache your check boxes in a variable and search that multiple times. That way you're only searching the check boxes each time instead of the whole DOM.

// in document ready
var checkboxes = $('input[type=checkbox]');
// When you need to find the checked check boxes
var checked = checkboxes.find(':checked:not(.checkall)');
answered Mar 6, 2013 at 8:38
\$\endgroup\$
0
4
\$\begingroup\$

Addionally to @Tyriar's answer:

You possibly could optimize the selection even more depending on where the checkboxes are in the HTML by making the selector more specific and possibly modifying the HTML to help with that.

For example, if all checkboxes are the same element, add it's ID (or class) as a parent in the selector:

$("#mytable input[type=checkbox]:checked:not(.checkall)")

Also :not isn't very efficient. If the "check all" checkboxes were, for example, in the thead of a table while the checkboxes you need are in the tbody then use:

$("#mytable tbody input[type=checkbox]:checked")
answered Mar 6, 2013 at 12:49
\$\endgroup\$
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.