javascript - Don't trigger JQuery on a disabled checkbox -


i have form checkboxes. when users click on label, have custom checkbox icon gets swapped. works fine, have disabled checkboxes in form, , don't wan't click event triggered when click on disabled ones. how do that?

jquery:

$('input[type=checkbox] + label').click(function() {   $(this).toggleclass('checkbox-pressed'); }); 

html:

<input type="checkbox" id="a"><label for="a">option 1</label> <input disabled type="checkbox" id="b"><label for="b">option 2 - don't trigger jq</label> 

use :not(:disabled) select checkboxes aren't disabled.

$('input[type=checkbox]:not(:disabled)').click(function() {     ... }); 

documentation: :not() , :disabled


you should rather use .on(). otherwise won't work checkboxes become disabled after page loads (say, because of user action):

$(document).on('click', 'input[type=checkbox]:not(:disabled)', function() {     ... }); 

Comments