show-notice
hide-notice

Thursday, 15 August 2013

jQuery to limit number of checkboxes checked by User


Introduction:

Many time we need to limit the selection of checkboxes users can select or in other words users are not allowed to select more than a number of checkbox to give their choice. For example, out of all client side script language, user can select maximum three where he is best.




As for example, take a look at below HTML checkboxlist


HTML:
 jQuery 

JavaScript 

 Prototype

 Dojo

 Mootools 



JS:
$(document).ready(function () {
   $("input[name='tech']").change(function () {
      var maxAllowed = 2;
      var cnt = $("input[name='tech']:checked").length;
      if (cnt > maxAllowed) 
      {
         $(this).prop("checked", "");
         alert('Select maximum ' + maxAllowed + ' technologies!');
     }
  });
});


SHARE THIS POST   

0 comments :

Post a Comment

Design by Gohilinfotech | www.gohilinfotech.blogspot.com