Check if checkbox is checked with jQuery
IDs must be unique in your document, meaning that you shouldn't do this:
<input type="checkbox" name="chk[]" id="chk[]" value="Apples" />
<input type="checkbox" name="chk[]" id="chk[]" value="Bananas" />
Instead, drop the ID, and then select them by name, or by a containing element:
<fieldset id="checkArray">
<input type="checkbox" name="chk[]" value="Apples" />
<input type="checkbox" name="chk[]" value="Bananas" />
</fieldset>
And now the jQuery:
var atLeastOneIsChecked = $('#checkArray:checkbox:checked').length > 0;
//there should be no space between identifier and selector
// or, without the container:
var atLeastOneIsChecked = $('input[name="chk[]"]:checked').length > 0;
How do I check whether a checkbox is checked in jQuery?
This worked for me:
$get("isAgeSelected ").checked == true
Where isAgeSelected
is the id of the control.
Also, @karim79's answer works fine. I am not sure what I missed at the time I tested it.
Note, this is answer uses Microsoft Ajax, not jQuery
Dynamically check if the checkbox is checked on-fly
You can use the querySelectorAll
function to get list of checked checkbox in specific div.
If size of list > 0
, it contains checked checkbox.
Check if one item or more item is checked under Doctor.
document.querySelectorAll('#doctor input[type=checkbox]:checked').length > 0
Check if one item or more item is checked under patient.
document.querySelectorAll('#patient input[type=checkbox]:checked').length > 0
adding style to li elements if checkbox is checked
Though DCR's solution is good, it doesn't work if the input gets unchecked. Here's a little modification that i've made, hope it will solve the problem!
function changeOnChecked(){
const a = event.target.closest('input').checked;
if(a)
event.target.closest('li').style.color='red'
else
event.target.closest('li').style.color='black'
}
<ul id = "to-do-list" >
<li> <input type="checkbox" onclick="changeOnChecked()"> to school</li>
<li> <input type="checkbox" onclick="changeOnChecked()"> do website</li>
</ul>
If checkbox is checked condition Javascript
I'm not sure what was going on but when I had if(checkbox == true)
I was getting the message saying true is not defined
as if it was looking for a variable called true
.
I changed it to using Jquery like so if($("#checkbox").is(':checked'))
How can i check and unchecked detect in checkboxes inside td using jquery or javascript DOM?
To get checkbox
inside clicked td
you need to write $(this).find("input:checkbox")
.
Also to get whether checkbox
is checked
you need to use .is(':checked')
.
Try it below.
$('td').click(function() {
var isChecked = $(this).find("input:checkbox").is(':checked');
if (isChecked) {
console.log('checked');
} else {
console.log('false');
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
<table id="tableModul" class="w-30 table table-lg table-hover table-bordered table-striped">
<thead class="thead-dark">
<tr>
<th>Department</th>
<th>Kode Unit</th>
<th style="width: 30px;">position_name</th>
</tr>
</thead>
<tbody id="myTable">
<tr>
<td class="nik">DEPARTEMEN</td>
<td class="nr">KODE UNIT</td>
<td>
<div class="form-check"><input type="checkbox" class="form-check-input checksboxs" style="zoom: 1.5; width:100%;"></div>
</td>
</tr>
<tr>
<td class="nik">DEPARTEMEN</td>
<td class="nr">KODE UNIT</td>
<td>
<div class="form-check"><input type="checkbox" class="form-check-input checksboxs" style="zoom: 1.5; width:100%;"></div>
</td>
</tr>
</tbody>
</table>
Check if all checkboxes are checked, then add class to something
You need to get the total number of checkboxes and increment a counter if a particular checkbox is ticked. If the numbers match, all checkboxes must be ticked.
function showMe (box) {
var checkbox = document.getElementsByName("selectPlant");
var counter=0;
for(var i=0;i<checkbox.length;i++) {
if(checkbox[i].checked){
counter++;
}
if(counter==checkbox.length)
{
document.getElementById(box).classList.add('show');
}
}
}
Related Topics
Allow Google Chrome to Use Xmlhttprequest to Load a Url from a Local File
Asynchronously Load Images with Jquery
Jquery's Jquery-1.10.2.Min.Map Is Triggering a 404 (Not Found)
Convert String with Commas to Array
Invariant Violation: Objects Are Not Valid as a React Child
How to Use Jquery for Xml Parsing with Namespaces
Format Numbers in JavaScript Similar to C#
Detecting When User Scrolls to Bottom of Div with Jquery
Make Axios Send Cookies in Its Requests Automatically
Call an Asynchronous JavaScript Function Synchronously
Difference Between Microtask and MACrotask Within an Event Loop Context
Jquery - Setting the Selected Value of a Select Control via Its Text Description
Iframe Src Change Event Detection
Defining Methods via Prototype VS Using This in the Constructor - Really a Performance Difference
What Is the Stability of the Array.Sort() Method in Different Browsers