Reputation: 43
I'm trying to resolve whether or not a checkbox is checked. It seems rather straight forward.
<script type="text/Javascript">
function ValidateReqNum() {
var zCheckBox = document.getElementById('chkAllJobs');
if (zCheckBox.checked)
alert("true");
if (!zCheckBox.checked)
alert("false");
return true;
}
</script>
and the checkbox:
<asp:CheckBox ID="chkAllJobs" runat="server" Text="All Jobs" />
called from:
<asp:Button ID="btnPrintReport" runat="server" Text="Run Report"
OnClientClick="return ValidateReqNum();" OnClick="CreatePDFJobReport" />
I've tried it dozens of different ways and it keeps coming back with
Error: Unable to get value of the property 'checked': object is null or undefined
My other elements in the same aspx page are reporting in just fine. I can call chkAllJobs from my c# code and I can resolve whether or not it's checked from c# as well.
Upvotes: 4
Views: 7440
Reputation: 33809
If you are using master pages, control ids of child page at client will be different to their server ids. So instead of using server control name, try using its client id as;
var zCheckBox = document.getElementById('<%= chkAllJobs.ClientID %>');
function ValidateReqNum() {
alert(zCheckBox.checked);
}
Upvotes: 3
Reputation: 5843
Here is a sample how check-box work. If you have the following check-box:
<input id="Checkbox" type="checkbox" name="mycheckbox" value="5"/>
Then you can get the value using forms collection
label_Result.text = Request.Form["mycheckbox"];
Consequently, you will get the value 5 only if that checkbox is checked.
Upvotes: 1