Home > Enterprise >  jQuery Loop Through dynamically Generated Checkboxes And Default Check Them All
jQuery Loop Through dynamically Generated Checkboxes And Default Check Them All

Time:05-06

I have a variable that gets the select all checkboxes and associated ID with them in another function:

  var $allPdM = $('#SelectAll_'   pdmID);

In the $(document).ready(function() Currently, I have hard these hard coded in the page. However, I would like to do it dynamically now that I know there will be more coming:

  $('#SelectAll_2006').prop("checked",true);
  $('#SelectAll_2007').prop("checked",true);
  $('#SelectAll_2008').prop("checked",true); 

This must be done on page load in the document.ready function.

CodePudding user response:

Give all the checkboxes a common class, and use that class when setting the property.

$(function() {
  $(".selectall").prop("checked", true);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" value="2006" id="SelectAll_2006" > 2006<br>
<input type="checkbox" value="2007" id="SelectAll_2007" > 2007<br>
<input type="checkbox" value="2008" id="SelectAll_2008" > 2008<br>
<input type="checkbox" value="2009" id="SelectAll_2009" > 2009<br>

  • Related