<form>
<input type="checkbox" name="newsletter" value="Hourly" checked="checked"> <input type="checkbox" name="newsletter" value="Daily">
<input type="checkbox" name="newsletter" value="Weekly" checked>
<input type="checkbox" name="newsletter" value="Monthly">
<input type="checkbox" name="newsletter" value="Yearly">
<br><button id="selectall">Select All</button>
</form>
<script>
var selectall = function() {
$("input[type=checkbox]").attr("checked", "true");
};
$("#selectall").on("click", selectall);
</script>
I have written a JQuery script which will select all the checkboxes when I click on the button "Select All". Now, after selection, I will manually uncheck all the checkboxes and then press "Select All". But this time it fails in selecting all the checkboxes. How to prevent this?