blue44
asked on
Need to hide and unhide text field with select box
Hi,
I have a select box that I'd like to enable a field to be visible or not. Basically, when you select 'yes', it will display the input field. If you select 'no', the field will go away.
My code is below.
Many thanks,
blue44
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<script language="JavaScript" type="text/JavaScript">
function DisplayFields()
{
document.ApplyUpdate.Worke rDescripti on.style.v isibility= "hidden";
}
</script>
</head>
<body>
<form action="ApplyHandler.htm" method="post" name="ApplyUpdate" >
<select name="Worker" onBlur="DisplayFields();">
<option value="Yes" >Yes</option>
<option value="No" >No</option>
</select>
<input style="VISIBILITY: visible" value="Good Worker" name="WorkerDescription" type="text" size="80" maxlength="80">
</form>
</body>
</html>
I have a select box that I'd like to enable a field to be visible or not. Basically, when you select 'yes', it will display the input field. If you select 'no', the field will go away.
My code is below.
Many thanks,
blue44
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<script language="JavaScript" type="text/JavaScript">
function DisplayFields()
{
document.ApplyUpdate.Worke
}
</script>
</head>
<body>
<form action="ApplyHandler.htm" method="post" name="ApplyUpdate" >
<select name="Worker" onBlur="DisplayFields();">
<option value="Yes" >Yes</option>
<option value="No" >No</option>
</select>
<input style="VISIBILITY: visible" value="Good Worker" name="WorkerDescription" type="text" size="80" maxlength="80">
</form>
</body>
</html>
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
devic,
Won't using display='' fix that? You don't make it a block-level element, so it'll simply restore itself in the flow of the document, no?
Won't using display='' fix that? You don't make it a block-level element, so it'll simply restore itself in the flow of the document, no?
dakyd,
yes display='' is correct syntax, and works on non IE browsers too.
I wanted just notice the difference between display and visibility. And is that display:none treats it like it does not exist, so space is not reserved for it. Visibility reserves the space for it, but just does not show it on the screen.
yes display='' is correct syntax, and works on non IE browsers too.
I wanted just notice the difference between display and visibility. And is that display:none treats it like it does not exist, so space is not reserved for it. Visibility reserves the space for it, but just does not show it on the screen.
ASKER
Thanks everyone. You solved a big issue for me!!
you welcome!
ASKER
One more question. Can you modify the script so that depending on the value in the select box, the field will be either hidden or visible? Also, using <div>, I'd like to make the label for the select box also hidden or visible.
I'll away an extra 200 pts for this.
Thanks!!
I'll away an extra 200 pts for this.
Thanks!!
here with div:
======================
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<script language="JavaScript" type="text/JavaScript">
function DisplayFields(obj)
{
document.getElementById("m yDiv").sty le.visibil ity=obj.se lectedInde x==0?"visi ble":"hidd en";
}
</script>
</head>
<body>
<form action="ApplyHandler.htm" method="post" name="ApplyUpdate" >
<select name="Worker" onchange="DisplayFields(th is);">
<option value="Yes" >Yes</option>
<option value="No" >No</option>
</select>
<div id=myDiv>
asdfghjk
<input value="Good Worker" name="WorkerDescription" type="text" size="80" maxlength="80">
</div>
</form>
</body>
</html>
======================
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<script language="JavaScript" type="text/JavaScript">
function DisplayFields(obj)
{
document.getElementById("m
}
</script>
</head>
<body>
<form action="ApplyHandler.htm" method="post" name="ApplyUpdate" >
<select name="Worker" onchange="DisplayFields(th
<option value="Yes" >Yes</option>
<option value="No" >No</option>
</select>
<div id=myDiv>
asdfghjk
<input value="Good Worker" name="WorkerDescription" type="text" size="80" maxlength="80">
</div>
</form>
</body>
</html>
ASKER
That works great, devic! Thanks!!!
Is there a way to turn on/off visibility depending on the option value?
Is there a way to turn on/off visibility depending on the option value?
hi blue44, of course yes ;)
========================== =
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<script language="JavaScript" type="text/JavaScript">
function DisplayFields(obj)
{
document.getElementById("m yDiv").sty le.visibil ity=obj.va lue=="Yes" ?"visible" :"hidden";
}
</script>
</head>
<body>
<form action="ApplyHandler.htm" method="post" name="ApplyUpdate" >
<select name="Worker" onchange="DisplayFields(th is);">
<option value="Yes" >Yes</option>
<option value="No" >No</option>
</select>
<div id=myDiv>
asdfghjk
<input value="Good Worker" name="WorkerDescription" type="text" size="80" maxlength="80">
</div>
</form>
</body>
</html>
==========================
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<script language="JavaScript" type="text/JavaScript">
function DisplayFields(obj)
{
document.getElementById("m
}
</script>
</head>
<body>
<form action="ApplyHandler.htm" method="post" name="ApplyUpdate" >
<select name="Worker" onchange="DisplayFields(th
<option value="Yes" >Yes</option>
<option value="No" >No</option>
</select>
<div id=myDiv>
asdfghjk
<input value="Good Worker" name="WorkerDescription" type="text" size="80" maxlength="80">
</div>
</form>
</body>
</html>
ASKER
That's incredible. You are a JS Genius!!
Being the novice that I am, I tried to understand your funciton by writing out the code in long format. I came up with the following, but it errors out. What am I missing?:
function DisplayFields(obj)
{
if (obj.value == "Yes")
{
document.getElementById("m yDiv").sty le.visibil ity = "visible";
else
{
document.getElementById("m yDiv").sty le.visibil ity = "hidden";
}
}
Being the novice that I am, I tried to understand your funciton by writing out the code in long format. I came up with the following, but it errors out. What am I missing?:
function DisplayFields(obj)
{
if (obj.value == "Yes")
{
document.getElementById("m
else
{
document.getElementById("m
}
}
try to keep nice formating and then you will see syntax errors. You forgot one "}"
========================== ==========
<script language="JavaScript" type="text/JavaScript">
function DisplayFields(obj)
{
if (obj.value == "Yes")
{
document.getElementById("m yDiv").sty le.visibil ity = "visible";
}
else
{
document.getElementById("m yDiv").sty le.visibil ity = "hidden";
}
}
</script>
==========================
<script language="JavaScript" type="text/JavaScript">
function DisplayFields(obj)
{
if (obj.value == "Yes")
{
document.getElementById("m
}
else
{
document.getElementById("m
}
}
</script>
ASKER
Arghhhh....but at least that makes sense.
Thanks agian, devic!!
Thanks agian, devic!!
<select name="Worker" onChange="if (this.selectedIndex == 0) document.ApplyUpdate.Worke
<option value="Yes" >Yes</option>
<option value="No" >No</option>
</select>
<input value="Good Worker" name="WorkerDescription" type="text" size="80" maxlength="80">