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>
ASPJavaScript

Avatar of undefined
Last Comment
hielo

8/22/2022 - Mon
sammySeltzer

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

hielo

javascript is case sensitive.  So the If and Else keywords should be all in lower case.
ASKER CERTIFIED SOLUTION
viralpala

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
dssisupport

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

Thanks!
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
hielo

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