Getting state of a checkbox

dssisupport
dssisupport used Ask the Experts™
on
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>
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2011

Commented:
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

Expert of the Year 2008
Top Expert 2008

Commented:
javascript is case sensitive.  So the If and Else keywords should be all in lower case.
Here your complete solution

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript">
        function RefreshScore(CtlID) {



            var result = CtlID.checked;
            if(result)
            {
                alert("N/A is checked");
               
            }
            else
            {
                alert("N/A is NOT checked");
            }
        }
</script>
</head>
<body>



<input id="Section1Question1NAValue" type="checkbox"  onclick="RefreshScore(this);"  /><span>N/A</span>


</body>
</html>

what I changed

1) if (previously it was If) and else  (previously it was Else)
2) Assign this
3) checking the value using this.

Let me know if it doesn't work.

Author

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

Thanks!
Expert of the Year 2008
Top Expert 2008

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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial