Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to limit the number of selected checkboxes?

I have the following HTML:

<div class="pricing-levels-3">    <p><strong>Which level would you like? (Select 3 Levels)</strong></p>    <input class="single-checkbox"type="checkbox" name="vehicle" value="Bike">Level 1<br>    <input class="single-checkbox" type="checkbox" name="vehicle" value="Bike">Level 2<br>    <input class="single-checkbox" type="checkbox" name="vehicle" value="Bike">Level 3<br>    <input class="single-checkbox" type="checkbox" name="vehicle" value="Bike">Level 4<br>    <input class="single-checkbox" type="checkbox" name="vehicle" value="Bike">Level 5<br>    <input class="single-checkbox" type="checkbox" name="vehicle" value="Bike">Level 6<br>    <input class="single-checkbox" type="checkbox" name="vehicle" value="Bike">Level 7<br> </div> 

Live site:

http://www.chineselearnonline.com/amember/signup20.php

How can I do it so that the user can only select 3 of those checkboxes?

like image 521
alexchenco Avatar asked Sep 25 '13 10:09

alexchenco


People also ask

How do I restrict Checkboxlist selection upto 3 in HTML?

click(function(){ if ($('. checkbox:checked'). length >= 3) { $(". checkbox").

How many checkboxes can be selected at a time?

Because more than one checkbox can be selected at a time, the name of each checkboxes must be unique so that each one can be identified separately. The values should also be unique in order to represent the value of each option.


1 Answers

Using change event you can do something like this:

var limit = 3; $('input.single-checkbox').on('change', function(evt) {    if($(this).siblings(':checked').length >= limit) {        this.checked = false;    } }); 

See this working demo

like image 161
letiagoalves Avatar answered Sep 19 '22 06:09

letiagoalves