4

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.

1
  • 1
    your error message is telling you that zCheckBox is undefined, look at @Kaf's answer, that should go the trick Commented Jan 4, 2013 at 0:30

2 Answers 2

3

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);
}
Sign up to request clarification or add additional context in comments.

3 Comments

That was it, thank you! It stumped me for a moment because it's "ClientID" but you're awesome. Much appreciated.
You are welcome ! I see, I have changed ClientId to ClientID.
You can also set the attribute ClientIDMode="Static". See: msdn.microsoft.com/en-us/library/…
1

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.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.