I have a list of checkboxes and I'm trying to limit to 2 max checkboxes by disabling all checkboxes without a mark after 2 were selected.
This works fine, but I try to display a message to the user if they click on the disabled checkbox to tell them why they cannot select more than 2. I try to fire the click() event on the disabled checkbox, but it actually doesn't fire. Any ideas?
var totalChecked = 0; var checkedLimit = 1; jQuery(".post-to-facebook").change(function() { if (jQuery(this).attr("checked") == "checked") { if (totalChecked < checkedLimit) { totalChecked += 1; if (totalChecked == checkedLimit) { jQuery(".post-to-facebook[checked!='checked']").attr("disabled", true); } } else { jQuery(this).attr("checked", false); alert("You can only post twice to Facebook at one time. This is to avoid spam complaints, we don't want to spam on your page!"); } } else { totalChecked -= 1; if (totalChecked < checkedLimit) { jQuery(".post-to-facebook[checked!='checked']").attr("disabled", false); } } console.log(totalChecked); });
Wasim source share