Link to home
Create AccountLog in
Avatar of dssisupport
dssisupport

asked on

Getting state of a checkbox

I am having a problem getting the checked state of a checkbox after an onclick event. I have verified that the onclick event triggers but no matter how I try and check the state of the element (by name or by id) I am not seeing anything coming back. Below is a copy of the function I am currently trying to use to get it by the element ID and also the checkbox html code. The form name is addeval, if that is relevant.

<script type="text/javascript">
function RefreshScore()
{
If(document.getElementById("Section1Question1NAValue").checked)
      {
       alert("N/A is checked");
       }      
Else
      {      
      alert("N/A is NOT checked");
      }
}
</script>


<input name="Section1Question1NAValue" id="Section1Question1NAValue" type="checkbox" onClick="RefreshScore()">&nbsp;<span class="auto-style8">N/A
</span>
Avatar of sammySeltzer
sammySeltzer
Flag of United States of America image

I don't know what you are trying to do but something this simple could suffice.

function RefreshScore(checked) {
  if ( checked )
     alert("N/A is checked");
  else
     alert("N/A is NOT checked");
}



<input name="Section1Question1NAValue" id="Section1Question1NAValue" type="checkbox" onClick="RefreshScore();" checked />

Open in new window


OR


function checkClick(cb) { 
  setTimeout(function() { 
    display("Clicked, new value = " + cb.checked); 
  }, 0); 
} 
<label><input type='checkbox' onchange=checkClick(this);'>Checkbox</label> 

Open in new window


OR

<script type="text/javascript>
function RefreshScore(checkbox) 
{ 
    if (checkbox.checked) 
    { 
        alert("a"); 
    } 
} 
</script>

//markup
<input type="checkbox" <input name="Section1Question1NAValue" id="Section1Question1NAValue"  onclick="RefreshScore(this)" /> 

Open in new window

Avatar of hielo
javascript is case sensitive.  So the If and Else keywords should be all in lower case.
ASKER CERTIFIED SOLUTION
Avatar of viralpala
viralpala
Flag of India image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of dssisupport
dssisupport

ASKER

My main problem was the case of if and else. I knew it was somehting stupid.

Thanks!
My main problem was the case of if and else
Which I pointed out above on post ID: 37992749