0

I need to get the value of a checkbox put on my form within a javascript function. This is the code i'm using right now:

var Excludeviv = document.getElementById("ctl00_ctl00_cphMain_cphMainMenu_chkExcludeviv").value;

i put an alert to check the value that Exludeviv contains. i always get "on", i don't understand. Can anyone help me on this ? Thanks in advance

1
  • You store the checkbox’s value in that variable at some point, but the variable won’t get updated automatically when the checkbox is clicked. You need to take care of that, e.g. by having the checkbox’s click event trigger a function that updates your Excludeviv variable. Commented Feb 17, 2012 at 11:53

4 Answers 4

2

Use .checked

var Excludeviv = document.getElementById("ctl00_ctl00_cphMain_cphMainMenu_chkExcludeviv").checked;
Sign up to request clarification or add additional context in comments.

Comments

2

You need to check for the checked attribute, not the value.

var Excludeviv = document.getElementById("...").checked;

value is the value of the attribute value, which never changes:

<input type="checkbox" value="on" />

If the user checks the checkbox, the checked attribute changes - the value stays the same!

Comments

0
var Excludeviv = document.getElementById('<%=yourCheckBox.ClientID %>') 

try like this.

  if(Excludeviv && Excludeviv.checked) 
   {
     //do something 
   }

Comments

0

If you want to check the status use 'checked' property of the checkbox.

In fact, when checkbox is unchecked form will not send usually the state of that checkbox, and if e.g. you have lots of checkboxes, their states will be in array with the name 'on'... so you can figure it out manually and set your own value on checkbox.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.