Check if Checkbox is checked based on name

user3837156 picture user3837156 · Nov 28, 2014 · Viewed 15k times · Source

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()">

Answer

Joce Pedno picture Joce Pedno · Nov 28, 2014

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) 
}
}