HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

Limited checkbox selection

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
selectioncheckboxlimited

Problem

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

HTML:



Script:

$("input.checkbox:checkbox:checked:not(.checkall)").each(function () {
    var checkbox = $(this), machineId = checkbox.val()
}

Solution

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)');

Code Snippets

$("input[type=checkbox]:checked:not(.checkall)")
// in document ready
var checkboxes = $('input[type=checkbox]');

// When you need to find the checked check boxes
var checked = checkboxes.find(':checked:not(.checkall)');

Context

StackExchange Code Review Q#23523, answer score: 6

Revisions (0)

No revisions yet.