Learning jQuery

Tips, techniques, and tutorials for the jQuery JavaScript library
  • Filter:
  • All
  • Beginner
  • Intermediate
  • Advanced
  • Tutorials
    • Begin with jQuery
    • Setting up jQuery UI
    • Start with jQuery Mobile
    • jQuery with ASP.NET
  • AngularJS
    • AngularJS Interview Questions
    • Free ebooks to learn AngularJS
    • Bootstrapping AngularJS
    • All AngularJS articles
  • Interview Questions
    • jQuery Interview Questions
    • AngularJS Interview Questions
  • Plugins
    • 200+ jQuery Plugin Collection
    • Popup window plugin
    • LightBox Plugins
    • Text Effect Plugins
    • Best jQuery Plugins-2013
    • Best jQuery Plugins-2012
    • All Plugins
  • jQuery Mobile
    • Learn jQuery Mobile
    • jQuery Mobile Plugins
  • JQuery
    • Mobile
    • Plugins
  • Javascript
  • Resources
    • Mootools
    • Tutorials
  • Gallery
Difference between $(‘div’) and $(‘
‘) in jQuery
Delay jQuery animation by few seconds

Apr 05 2013

jQuery to limit number of checkboxes checked by User

by vikas

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.

Related Post:

  • Check if Checkbox is checked using jQuery
  • Validate HTML CheckboxList using jQuery
  • Check/Uncheck All Checkboxes with JQuery

As for example, take a look at below HTML checkboxlist.

<input type="checkbox" name="tech" value="jQuery" /> jQuery <br/>
<input type="checkbox" name="tech" value="JavaScript" />JavaScript <br/>
<input type="checkbox" name="tech" value="Prototype" /> Prototype<br/>
<input type="checkbox" name="tech" value="Dojo" /> Dojo<br/>
<input type="checkbox" name="tech" value="Mootools" /> Mootools <br/>

And now, you want to restrict user to select only 2 scripting language out of above group. Below jQuery code exactly does the same thing.

$(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!');
     }
  });
});

The idea is simple. Find out number of checked checkboxes and if they are greater than maximum allowed then alert the user.

See Complete Code

Feel free to contact me for any help related to jQuery, I will gladly help you.


Scripts Used on This Site

Popular Posts

  • Using jQuery's Data APIs
  • Merging jQuery Deferreds and .animate()
  • A jQuery UI Combobox Under the Hood
  • Quick Tip: Prevent Animation Queue Buildup
  • Working with Events, part 1
  • Improved Animated Scrolling Script for Same-Page Links

Navigation

  • Home
  • Categories
  • Archives
  • About
  • Contact

Popular Posts

  • Using jQuery's Data APIs
  • Merging jQuery Deferreds and .animate()
  • A jQuery UI Combobox Under the Hood
  • Quick Tip: Prevent Animation Queue Buildup
  • Working with Events, part 1
  • Improved Animated Scrolling Script for Same-Page Links

Recommended Book

  • Learning jQuery, Fourth Edition book
    Learning jQuery,
    Fourth Edition
    By Karl Swedberg &
    Jonathan Chaffer

Links

Take A Look At Our Achives!

What Is This?

Learning jQuery is a multi-author weblog providing jQuery tutorials, demos, and announcements. We have tutorials for all skill levels, and each entry is categorized by level of difficulty.

Pages

  • Home
  • Categories
  • Archives
  • About
  • Contact
  • Cookies and privacy policy

RSS

  • Entries (RSS)
  • Comments (RSS)
Creative Commons License

© Copyright 2006–2025 Learning jQuery and participating authors. Written content on this site is under a Creative Commons License. Code examples are under your choice of MIT or GPL license.

Development & Design by Landocs with WordPress

Responsive Menu
Add more content here...