Click here to Skip to main content
15,900,906 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Guys I Have Following UI
1)
I have To Div In one page
each Div Contain
TextBox and
One Check Box
And Button
On BUtton Click It Will Check That CheckBox Is Checked Or Not.
For That I write Javascript.

2) @nd Div Contain Same thing.
want to d same opertion

3)
I m Passing ckebox Id for javascript function on button click.

but it show null value.

code arte as follow:

HTML
<div id="div1">
    <textbox>
    <check box id="checkbox1">
   <button  önclientckick="ischecked(checkbox1)">

</div>
<div id="div2">
    <textbox>
    <check box id="checkbox2">
   <button  önclientckick="ischecked(checkbox2)">

</div>


now
javascript function

JavaScript
function  IsChecked(Id)
{
   alert(Id);
  var chk = document.getElementById(Id);
       alert(chk);//this will give null value..
  if(!chk.checked)
  {
  alert("Please check Terms and Condition checkbox");
  return false;
  } 
  else
  {
  return confirm("Are you sure want to submit");
  }

} 

alert(chk);//this will give null value..
what should i do??????
Posted
Updated 15-Dec-11 18:17pm
v3

Try the below..
HTML
<div id="div1">
    <input type="text"/>
    <input type="checkbox" id="checkbox1"/>
   <input type="button" onclick="ischecked('checkbox1');" value="click1" />

    </div>
    <div id="div2">
    <input type="text"/>
    <input type="checkbox" id="checkbox2"/>
   <input type="button" onclick="ischecked('checkbox2');" value="click1" />

   </div>

JavaScript
function  ischecked(Id)
{
   alert(Id);
  var chk = document.getElementById(Id);
       alert(chk);//this will give null value..
  if(!chk.checked)
  {
  alert("Please check Terms and Condition checkbox");
  return false;
  }
  else
  {
  return confirm("Are you sure want to submit");
  }

}


Thanks..
 
Share this answer
 
Comments
Balakrishnan Dhinakaran 16-Dec-11 0:57am    
while executing you ll get this error [object HTMLInputElement]
Shobana16 16-Dec-11 1:06am    
We need to check whether it is checked or not.. thats enough na..we get this alert("Please check Terms and Condition checkbox") , if it is not clicked. Am i right?
Shobana16 16-Dec-11 1:12am    
I think alert(chk); is the unwanted line..Because we can get the id of checkbox as a prameter.If we want to get its value means , we can put like this
alert(chk.value);
Balakrishnan Dhinakaran 16-Dec-11 1:15am    
you please check my code ..:)
Shobana16 16-Dec-11 1:23am    
ya sure..
This works fine

XML
<html>
<head>
<title>
chkbx
</title>
<script>
function  ischecked(Id)
{
  var chk = document.getElementById(Id);
  if(!chk.checked)
  {
  alert("Please check Terms and Condition "+Id);
  return false;
  }
  else
  {
  return confirm("Are you sure want to submit "+Id);
  }
 
}
</script>
</head>
<body>
<div id="div1">
    <input type="text"/>
    <input type="checkbox" id="checkbox1"/>
   <input type="button"  önclick="return ischecked('checkbox1');" value="click1" />
    </div>
    <div id="div2">
    <input type="text"/>
    <input type="checkbox" id="checkbox2"/>
   <input type="button"  önclick="return ischecked('checkbox2');" value="click2" /> 
   </div>
</body>
</html>
 
Share this answer
 
v2
Comments
Shobana16 16-Dec-11 1:29am    
Hey,
That is the error in "onclick". 'o' is not clear.

Otherwise it is correct..
Balakrishnan Dhinakaran 16-Dec-11 3:48am    
oh ..That happened while entering into codeproject
var chk = document.getElementById(objId);
Give Null Value.
There fore it can not go further!.
for either it is checked or not!
 
Share this answer
 
which button you are using asp:button or HTMLInput. check the following code
ASP.NET
div id="div1">
   <asp:checkbox runat="server" id="checkbox1" text="blah">
   <asp:button runat="server" onclientckick="return ischecked(checkbox1)" text="click">
 
</div>
<div id="div2">
   &lt;asp:checkbox runat="server" id="checkbox2" text="blah"&gt;
   &lt;asp:button runat="server" onclientckick="return ischecked(checkbox2)" text="click">
 </div>


in Javascript
JavaScript
function ischecked(objID)
{
  var chk = document.getElementById(objId);
  if(!chk.checked)
  {
  alert("Please check Terms and Condition checkbox");
  return false;
  }
  else
  {
     if (!(confirm("Are you sure want to submit")))
     {
        alert("you have clicked Cancel");
        return false;
     }
  }

}
 
Share this answer
 
Comments
[no name] 16-Dec-11 2:52am    
chk will give null value.
should i used checkboxId.ClientId Here!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900