new_to_php
asked on
DHTML To make a text field appear in a field
I am making a simple form. I want to add a check box that when checked makes a text box appear below it. When unchecked the field disappears.
I know its a simple process but I've seen so many ways to do it I thought I'd ask for a nice tight method. I want the structure to remain in the same form.. so hiding a div etc is likley the method I'm looking for.
TiA
I know its a simple process but I've seen so many ways to do it I thought I'd ask for a nice tight method. I want the structure to remain in the same form.. so hiding a div etc is likley the method I'm looking for.
TiA
ASKER
Sorry..
I should have mentioned I'm running PHP
I'm looking for a show hide div type DHTML code that fires when the check box is clicked
I should have mentioned I'm running PHP
I'm looking for a show hide div type DHTML code that fires when the check box is clicked
Try this
There is no need to server side scripting
There is no need to server side scripting
k.. try this,
<INPUT TYPE="checkbox" NAME="switchBox"
onclick="showHideText(this ,'myText') ">
Next you'll need a div that contains the text you want to show/hide,
like this:
<div ID="myText">Put text here</div>
Then you'll need the javascript function to hide the text, like this:
function showHideText(boxName,divNa me) {
if(boxName.checked = true) {
document.getElementById(di vName).sty le.display = 'block';
}
else {
document.getElementById(di vName).sty le.display = 'none';
}
}
<INPUT TYPE="checkbox" NAME="switchBox"
onclick="showHideText(this
Next you'll need a div that contains the text you want to show/hide,
like this:
<div ID="myText">Put text here</div>
Then you'll need the javascript function to hide the text, like this:
function showHideText(boxName,divNa
if(boxName.checked = true) {
document.getElementById(di
}
else {
document.getElementById(di
}
}
<html>
<head>
<title>JS Example</title>
<script type="text/javascript">
function ShowCtrl(box, el) {
var elem = document.getElementById(el);
if(box.checked) {
elem.style.display = '';
}
else
{
elem.style.display = 'none';
}
}
</script>
</head>
<body >
<form id="form">
<input id="checkbox1" type="checkbox" onclick="ShowCtrl(this, 'input1')" />
<br />
<input type="text" id="input1" style="display:none;" />
</form>
</body>
</html>
ASKER
remorina,
It works but is there a way to encapsulate the label? Like in a div for instance?
It works but is there a way to encapsulate the label? Like in a div for instance?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
One other question.. is there a way to make it so the div etc doesn't load on refresh? It would be nice if it defaults to hidden
Dear, this would be the simplest way to do that..
<input type=checkbox checked onclick="txtBox.style.visi bility = (this.checked)?'visible':' hidden'">s how/hide
<input type=text name=txtBox value="this is the box">
if you still want div to be used? try this:
<input type=checkbox checked onclick="document.getEleme ntById('my BoxContain er').style .visibilit y = (this.checked)?'visible':' hidden'">s how/hide
<span id="myBoxContainer">
<input type=text name=txtBox value="this is the box">
</span>
replace "span" can be replaced to "div" if you compromise line brake.
<input type=checkbox checked onclick="txtBox.style.visi
<input type=text name=txtBox value="this is the box">
if you still want div to be used? try this:
<input type=checkbox checked onclick="document.getEleme
<span id="myBoxContainer">
<input type=text name=txtBox value="this is the box">
</span>
replace "span" can be replaced to "div" if you compromise line brake.
<input type=checkbox checked onclick="txtBox.style.visibility=(this.checked)?'visible':'hidden'">show/hide
<input type=text name=txtBox value="this is the box">
<input type=checkbox checked onclick="document.getElementById('myBoxContainer').style.visibility = (this.checked)?'visible':'hidden'">show/hide
<span id="myBoxContainer">
<input type=text name=txtBox value="this is the box">
</span>
ASKER
Appreciate the follow through senthurpandian
I have it figured out now.. thanks to all..
I have it figured out now.. thanks to all..
Open in new window