• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Enabling and disabling Check Boxes

 
Krish Rarhi
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<script language="javascript">
function chMd()
{
// initialize form with empty field
document.forms[0].text1.disabled=false;
document.forms[0].text1.value="";

document.forms[0].text2.disabled=false;
document.forms[0].text2.value="";

document.forms[0].text3.disabled=false;
document.forms[0].text3.value="";

document.forms[0].goServer.disabled=false;
for(var i=0;i<document.forms[0].elements.length;i++)
{
if(document.forms[0].elements[i].name=="checkbox")
{
if(document.forms[0].elements[i].value=="c1")
{
if(document.forms[0].elements[i].checked==true){

document.forms[0].text1.disabled=false;
document.forms[0].text2.disabled=true;
document.forms[0].text3.disabled=true;



document.forms[0].goServer.disabled=true;
}
if(document.forms[0].elements[i].value=="c2")
if(document.forms[0].elements[i].checked==true)
{
document.forms[0].text2.disabled=false;
}

if(document.forms[0].elements[i].value=="c3")
if(document.forms[0].elements[i].checked==true)
{
document.forms[0].text3.disabled=false;
}
}

else if(document.forms[0].elements[i].value=="c2")
{
if(document.forms[0].elements[i].checked==true){
document.forms[0].test2.disabled=false;
document.forms[0].Text1.disabled=true;
document.forms[0].Text3.disabled=true;
document.forms[0].goServer.disabled=false;
}
if(document.forms[0].elements[i].value=="c1")
if(document.forms[0].elements[i].checked==true)
{
document.forms[0].text1.disabled=false;
}

if(document.forms[0].elements[i].value=="c3")
if(document.forms[0].elements[i].checked==true)
{
document.forms[0].text3.disabled=false;
}
}
else if(document.forms[0].elements[i].value=="c3")
{
if(document.forms[0].elements[i].checked==true){
document.forms[0].text3.disabled=false;
document.forms[0].Text2.disabled=true;
document.forms[0].Text1.disabled=true;

document.forms[0].goServer.disabled=false;
}
if(document.forms[0].elements[i].value=="c1")
if(document.forms[0].elements[i].checked==true)
{
document.forms[0].text1.disabled=false;
}

if(document.forms[0].elements[i].value=="c2")
if(document.forms[0].elements[i].checked==true)
{
document.forms[0].text2.disabled=false;
}
}
}
}
}




></script>
<body>
<form id="form1" name="form1" method="post" action="">
<label>
<input type="checkbox" name="checkbox" value="c1" onClick="chMd()"/>
Firstname</label>
<label>
<input type="text" name="text1" disabled="disabled"/>
</label>


<label>
<input type="checkbox" name="checkbox" value="c2" onClick="chMd()"/>
Lastname</label>
<label>
<input type="text" name="text2" disabled="disabled"/>
</label>



<label>
<input type="checkbox" name="checkbox" value="c3" onClick="chMd()" />
Address</label>
<label>
<input type="text" name="text3" disabled="disabled" />
</label>



<label>
<input type="submit" name="Submit" value="Submit" />
</label>


</form>
</body>
</html>


I have provided my code. What i want to do is, i want to disable all other check boxes along with text boxes, when i go for check box 1, when i check the check box2, check box along with text box 1 and 2 both will be enabled. and similarly for check box 3.Can anybody please help me. Thank You in advance.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Firstly, please be sure to use code tags when posting code to the forums. Unformatted or unindented code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please click this link ⇒ UseCodeTags ⇐ for more information.

Properly indented and formatted code greatly increases the probability that your question will get quicker, better answers.

Secondly, this is something that you'd do with JavaScript, not JSP. As such, this topic has been moved to the JavaScript forum.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You provided a lot of code, but did not indicate what problems you are having with it. What aspect are you having trouble with?

And... document.forms[0] is an antiqauted and fragile way to address ewlements. Time to modernize your code a bit and use id values and document.getElementById().
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds all to similar to this. From brand new members, no less. Homework?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic