Check if Checkbox is checked based on name
Solution 1
Not working:
Since you use jQuery, you can check if any checkboxes are not checked:
function rstSelfs() {
var chkds = $("input[name='selfs']:checkbox");
if (chks.not(":checked").length > 0) {
return false;
} else {
($('.CssSel').get(0).selectedIndex = 0)
}
}
this worked but I had to rework it a bit! thank you!
Tweaked, working:
function rstSelfs() {
var chkds = $("input[name='selfs']:checkbox");
if (chkds.is(":checked")) {
return false;
} else {
($('.CssSel').get(0).selectedIndex = 0)
}
}
Solution 2
getElementsByName()
function returns an array of elements, not single element, so it doesn't have property checked
. If there is only one element by this name you should use something like this:
document.getElementsByName("selfs")[0].checked
Or to correct your function:
function rstSelfs() {
var chkds = document.getElementsByName('selfs')[0];
if(chkds.checked) {
alert ("something is checked"); }
else {
alert("nothing is checked");
}}
user3837156
Updated on June 04, 2022Comments
-
user3837156 almost 2 years
This is my code and it always returns "nothing is checked" no matter if something is checked or not.
function rstSelfs() { var chkds = document.getElementsByName('selfs'); if(chkds.checked) { alert ("something is checked"); } else { alert("nothing is checked"); }}
I have been using the above as a test to see if I can get the code to work properly. I have multiple checkboxes with the name "selfs" and I want the dropdown selection only to reset if none of the boxes with the name "selfs" are not checked. I would assume the code would look like this but nor this or the code above works... Please help.. I new to this and I have attempted to search and it is possible this has been answered but I apologize it did not name sent to me.
What I want the code to do is this:
function rstSelfs() { var chkds = document.getElementsByName('selfs'); if(chkds.checked) { return false; } else { ($('.CssSel').get(0).selectedIndex = 0) } } <form id="Service"> <input type="checkbox" name="site" id="cktmcom" onclick="isTsite()"> <input type="checkbox" name="selfs" id="ckselfc" onclick="isTsserv();isTextServC()"> <label for="checkbox">Self-Serve Classic</label><br> <input type="checkbox" name="selfs" id="ckselfn" onclick="isTsserv();isTextServN()"> <label for="checkbox">Self-Serve Next-Gen</label><br> <input type="checkbox" name="homeSer" id="ckhomes" onclick="isThs()"> <label for="checkbox">Home Services</label><br> <input type="checkbox" name="mapp" id="ckmobilea" onclick="isTmApp()"> <label for="checkbox">Mobile App</label><br> <input type="checkbox" name="checkbox" id="ckgem" onclick="isTextGem()"> <label for="checkbox">Gemini</label><br> </form> <form id="Service2"> <input type="checkbox" name="site" id="ckkmcom" onclick="isKsite()"> <label for="checkbox">mobile.com</label><br> <input type="checkbox" name="selfs" id="ckKselfc" onClick="isKsserv();isKServC()"> <label for="checkbox">M Self-Serve Classic</label><br> <input type="checkbox" name="selfs" id="ckKselfn" onClick="isKsserv();isKServN()"> <label for="checkbox">M Self-Serve Next-Gen</label><br> <input type="checkbox" name="mapp" id="ckKmobilea" onclick="isKmApp()"> <label for="checkbox">M Mobile App</label><br> </form>
The checkboxes with name "selfs" control the hide and show for the div = selfserve. And I need to reset the drop down selection only if none of the "selfs" are chcked . I have actually have 5 checkboxes that have the name selfs. The code is rather large thats why the other 3 are missing.
The HTML dropdown code is:
<div id="selfserve" style="display:none"> <select class="CssSel" style="width:175px" onChange="ssPicker()"> <option value="1" selected style="color:red;" ssSel="**Self-Serve Issue**">Select Self-Serve Issue</option> <option value="2" ssSel="Entire Site Down">Entire Site Down</option> <option value="3" ssSel="Login Issues">Login Issues</option> <option value="4" ssSel="Usage Meter Issue">Usage Meter Issue</option> <option value="5" ssSel="Change feature Issues">Change feature Issues</option> <option value="6" ssSel="Page Loading Issues">Page Loading Issues</option> <option value="7" ssSel="Extreme Latency">Extreme Latency</option> <option value="8" ssSel="Navigation Issues">Navigation Issues</option> </select> </div> <input type="button" name="reset_self" value="Reset" onClick="rstSelfs()">
-
user3837156 over 9 yearsI attempted to use this but did not work. I corrected the error with the var name as in the if statement it is written as chks rather than chkds.
-
user3837156 over 9 yearsI do have multiple elements with that name so how would I go about checking?
-
user3837156 over 9 yearsUncaught TypeError: Cannot read property 'selfs' of undefined for (i=0; i<document.telusService.selfs.length;i++) {
-
Jitesh over 9 yearswhat is telusService - I do not see that in your HTML code. Maybe try this in for loop :- document.getElementById("id_of_form_which_contains_selfs").selfs.length instead of document.Service.selfs.length .
-
user3837156 over 9 yearssorry I renamed it in my code but i did use <document.Service.length... but I also assume this code when it works will only reset it for the form named Service but will not reset it for the form that is named Service2
-
user3837156 over 9 yearsi changed it to document.getelementbyid and it just resets the dropdown regardless if something is checked or not.
-
user3837156 over 9 yearsAnyone know why this code doesn't work? This makes the most sense to me but does nothing