JavaScript Limit Selectable DOM Checkboxes

I am trying to limit the number of checkboxes a user can select. These checkboxes are DOM input objects generated for each item in an array. I'm having no luck with this currently, so any help is much appreciated. Thanks!

Fiddle Here: http://jsfiddle.net/vVxM2/222/

names =["Donny","Danny","Ricky","Eric","Jamie","Bobby","Booby"];
var numberOf = names.length; 
var text = "<ul>";
for (i = 0; i < numberOf; i++) {
    text += "<li class='playerListItem'><label><input type='checkbox' class='playerCheckbox'>" + names[i] + "</label></li>";
    }
text += "</ul>";
document.getElementById("recentPlayersContainer").innerHTML = text;


var limit = 3;
$('input.playerCheckbox').on('change', function(event) {
if($(this).siblings(':checked').length >= limit) {
   this.checked = false;
}
});
Answer:1

Your problem is in the change event.

Instead of doing:

if($(this).siblings(':checked').length >= limit)

You should do this:

if($('.playerCheckbox:checked').length >= limit)

Also, if your limit is maximum 3 checked, then you should do:

$('.playerCheckbox:checked').length > limit

Because when the event change is raised, the current checkbox is already checked.

Answer:2

Your problem was found here: if($(this).parent().siblings().children(":checkbox:checked").length >= limit). You actually have two parents, so you should have added another parent() and children() function. It should look like if($(this).parent().parent().siblings().children().children(":checkbox:checked").length >= limit)

names = ["Donny", "Danny", "Ricky", "Eric", "Jamie", "Bobby", "Booby"];
var numberOf = names.length;
var text = "<ul>";
for (i = 0; i < numberOf; i++) {
  text += "<li class='playerListItem'><label><input type='checkbox' class='playerCheckbox'>" + names[i] + "</label></li>";
}
text += "</ul>";
document.getElementById("recentPlayersContainer").innerHTML = text;


var limit = 3;
$('input.playerCheckbox').on('change', function() {
  if ($(this).parent().parent().siblings().children().children(":checkbox:checked").length >= limit) {
    $(this).attr('checked', false);
    alert('You can only select 3 checkboxes.');
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="recentPlayersContainer">
</div>
Answer:3

I'm working on a web app which implements a wizard-like behavior. it uses an API to get the "wizard" steps. the API works in a way where you send a request with the current step and all previous ...

I'm working on a web app which implements a wizard-like behavior. it uses an API to get the "wizard" steps. the API works in a way where you send a request with the current step and all previous ...

  1. angular router dynamic path
  2. angular router dynamic routes
  3. angular router dynamic data
  4. angular router dynamic component
  5. angular router dynamic redirect
  6. angular router dynamic
  7. angular router dynamic parameters
  8. angular router dynamic redirectto
  9. angular router dynamic url
  10. angular router dynamic link
  11. angular routing dynamic url
  12. angular routing dynamic
  13. angular routing dynamic path
  14. angular routing dynamic component
  15. angular routing dynamic data
  16. angular routing dynamic parameter
  17. angular routing dynamic template
  18. angular dynamic router outlet
  19. angular dynamic router outlet name
  20. angular dynamic router module

I am using modal window for the upload of images. But when click on upload I want to upload the images and then close the modal window. When I used data-dismiss="modal" as attribute in button it just ...

I am using modal window for the upload of images. But when click on upload I want to upload the images and then close the modal window. When I used data-dismiss="modal" as attribute in button it just ...

  1. close bootstrap modal with javascript
  2. close bootstrap modal jquery
  3. close bootstrap modal programmatically
  4. close bootstrap modal on submit
  5. close bootstrap modal on button click
  6. close bootstrap modal angular
  7. close bootstrap modal without jquery
  8. close bootstrap modal typescript
  9. close bootstrap modal on click outside
  10. close bootstrap modal in angular 7
  11. close bootstrap modal on click
  12. close bootstrap modal vuejs
  13. close bootstrap modal in react
  14. close bootstrap modal escape key
  15. close bootstrap modal angular 8
  16. close bootstrap modal on ajax success
  17. close bootstrap modal after ajax success
  18. close bootstrap modal automatically
  19. close bootstrap modal after save
  20. close bootstrap modal from angular component

I'm (poorly) attempting to create a simple page which loops between a clock + date, current weather, 7-day weather forecast, news and calendar events. Each of these 'items' are called upon using ...

I'm (poorly) attempting to create a simple page which loops between a clock + date, current weather, 7-day weather forecast, news and calendar events. Each of these 'items' are called upon using ...

  1. javascript loop call function
  2. javascript loop call stack
  3. callback for loop javascript
  4. javascript event loop call stack
  5. javascript loop ajax call
  6. javascript loop through call stack
  7. javascript for loop api call

I have an array of Javascript datetime objects which I want to display on a cal-heatmap. For this, i've done the following: var startTimes = [] //array of datetimes to show var cal = new CalHeatMap();...

I have an array of Javascript datetime objects which I want to display on a cal-heatmap. For this, i've done the following: var startTimes = [] //array of datetimes to show var cal = new CalHeatMap();...