2

I have two checkboxes. When I checked both checkboxes, I want to generate an alert "Passed". I have written like this

<html xmlns="http://www.w3.org/1999/xhtml">
 <head runat="server">
<title>Untitled Page</title> 
 
  <script type="text/javascript" >
   function validate() {
    if (document.getElementById('CheckBox1').checked &&   document.getElementById('CheckBox2').checked) {

        alert("passed");

     } else {
        alert("Referred")
    }
}
</script>


   <asp:CheckBox ID="CheckBox1" runat="server"  OnCheckedChanged="javascript:validate();" />
   <asp:CheckBox ID="CheckBox2" runat="server"  OnCheckedChanged="javascript:validate();" />

but the problem is this (checkbox1).checked is not getting inbuildly within the . how can I get it? is there any need for adding extra properties to my project for getting the .checked property? it's first time I'm using javascript. and my visualstudio version is 3.5.please help

2

2 Answers 2

8

OnCheckedChanged is a server side event, and you are handing it on the client side.

<asp:CheckBox ID="CheckBox1" runat="server"  onchange="validate();" />
<asp:CheckBox ID="CheckBox2" runat="server"  onchange="validate();" />

Also you might need to use .ClientID -

(document.getElementById('<%=CheckBox1.ClientID%>').checked &&   document.getElementById('<%=CheckBox2.ClientID%>').checked)
Sign up to request clarification or add additional context in comments.

Comments

-1

try this..

(in html)

<input type="checkbox" id="checkbox1" value="checkbox1">
<input type="checkbox" id="checkbox2" value="checkbox2">    

(in js)

if(($('#checkbox1').attr('checked'))&&($('#checkbox2').attr('checked')) ){
  alert("passed");
} else {
  alert("Referred");    
}

1 Comment

The OP isn't using jQuery.

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.