2

I have two checkboxes.when i checked both the checkboxes i wants 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 ther any need for adding extra properties to my project for getting the '.checked' property?its first time i'am using javascript. and my visualstudio version is 3.5.please help

Amit
  • 15,217
  • 8
  • 46
  • 68
ARATHY
  • 349
  • 5
  • 13
  • 29
  • 1
    did you checked html source code ? – rab Aug 19 '13 at 07:31
  • here is the answer check once the post http://stackoverflow.com/questions/5539139/change-get-check-state-of-checkbox http://stackoverflow.com/questions/9887360/check-if-checkbox-is-checked-javascript – Ramakrishna.p Aug 19 '13 at 07:45

2 Answers2

6

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)
Abijeet Patro
  • 2,842
  • 4
  • 37
  • 64
-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");    
}
mj930112
  • 80
  • 1
  • 10