Select and deselect all checkboxes

var thisEl      = $(e.target),
thisVal     = thisEl.val(),
_class      = thisEl.closest('ul').attr('class'),
selChckbox  = $('ul.'+ _class +' input[type="checkbox"]'),
valLiLen    = $('ul.'+ _class +' li').length - 1,
checkbox    = $('ul.'+ _class +' input[type="checkbox"]');

if (thisVal === 'all') {
if (thisEl.is(':checked') === true) { checkbox.prop('checked', true); }
else { checkbox.prop('checked', false); }
} else {
if (thisEl.is(':checked') !== true) {
$('ul.'+ _class +' li:nth-child(1) input[value="all"]').prop('checked', false);
} else {
if ($('ul.'+ _class +' input[type="checkbox"]:checked').length === valLiLen) {
$('ul.'+ _class +' input[type="checkbox"]').prop('checked', true);
}
}
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s