if (this.checked != true) {
this.checked = true;
}
else {
this.checked = false;
}
Above code enables Check up All and Check out All function. However, I found that if any of check box is reversely checked by user, every output reverses.
How should I change in order to acheive followings when button is clicked?
all boxes are checked (Acheive)Belows is the full code:
function display() {
$.ajax({
url: "test.php",
type: "get",
data: {
a: $('#selectest option:selected').val()
}
}).done(function(data) {
$('#result').text(data);
alert("Checking test");
$('input:checkbox[class='+data+']').each(function() {
if(this.checked != true){
this.checked = true;
}
else{
this.checked = false;
}
});
});
}
Was the user name Undefined actually available, or did you find a glitch in the matrix?
Your question contradicts your code. This answer is for your question. Your code appears to be setting checkbox states from an ajax call, which is something entirely different.
Since you're using jQuery, you can use the :checked selector. Then you can just check if the number of checked checkboxes is equal to the total number of checkboxes, and toggle .prop('checked') accordingly.
In this snippet, if all checkboxes are checked, all checkboxes will be unchecked (item 3). If not all checkboxes are checked, all checkboxes will be checked (item 1 and 2).
$('button').on('click', function() {
$('input[type="checkbox"]').prop('checked',
$('input[type="checkbox"]:checked').length !==
$('input[type="checkbox"]').length
);
});
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<button>toggle</button>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>