changing checkbox value programmatically.


Topic: changing checkbox value programmatically.

pro asked 7 years ago

I have the checkboxes setup nicely - and and they work properly. I have a 'select all' option which also works surprisingly!) from a non-MDB version of the page, using this code:
$("#selectAll").on("click", function(){
     $("input.studentListing").attr('checked','checked'); 
});
But - if i uncheck one of the boxes (manually) - and then try re-click on 'select all'  it no longer works. is this something you can fix? Thanks  

Zachary Bell pro premium priority answered 7 years ago

I had this problem too and someone on my team figured out the solution. You need to use .prop instead .attr in your js code. Explanation as to why here: https://stackoverflow.com/a/16051519/4488889  
//set all check boxes to checked
$("input:checkbox").prop("checked", true);

//check to see if all checkboxes are checked as boolean
$("input:checkbox").prop("checked");

//set all checkboxes to not checked
$("input:checkbox").prop("checked", false);

Please insert min. 20 characters.

FREE CONSULTATION

Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.

Status

Answered

Specification of the issue
  • User: Pro
  • Premium support: No
  • Technology: MDB jQuery
  • MDB Version: -
  • Device: -
  • Browser: -
  • OS: -
  • Provided sample code: No
  • Provided link: No