aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Enabling and disabling Check Boxes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Enabling and disabling Check Boxes" Watch "Enabling and disabling Check Boxes" New topic
Author

Enabling and disabling Check Boxes

Krish Rarhi
Greenhorn

Joined: Oct 11, 2010
Posts: 1
<!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

Joined: Jan 10, 2002
Posts: 61201
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61201
    
  66

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
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Sounds all to similar to this. From brand new members, no less. Homework?


GenRocket - Experts at Building Test Data
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Enabling and disabling Check Boxes