No Comments

Author: samdiaz Posted On: April 2nd, 2013 In:Tips and Tricks

Working on a new project for my job, I came across a need for limiting the number of selected checkboxes in a form. After searching online for a bit, I found this script which worked perfectly!

[cc lang="javascript"]

// when user updates a checkbox.
$(“input:checkbox”).change(function(){

// total allowed to be checked.
var max_allowed = 2;
// count how many checked.
var checked = $(“input:checked”).size();

// perform test.
if ( checked > max_allowed ) {

// is more than the max so uncheck.
$(this).attr(“checked”, false);

// display error message.
alert(“Please select a maximum of ” + max_allowed + ” options.”);

}

});

[cc]

(Source: Jim Lam)


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>