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
new_to_phpAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

krunal_shahCommented:
try the code below,
<%@ Page language="c#" AutoEventWireup="false" %>
<HTML>
<HEAD>
<title>Javascript Show Hide</title>

<script language=javascript>
function showHide(checkbox,textboxId)
{
var textbox = document.getElementById(textboxId);
if(checkbox.checked)
{
textbox.style.display = '';
}
else
{
textbox.style.display = 'none';
}
}
</script>
<script language=C# runat=server>
override protected void OnInit(EventArgs e)
{
chbShowHide.Attributes.Add("onClick",string.Format ("javascript:showHide(this,'{0}');",txtShowHide.ClientID));
base.OnInit(e);
}
</script>
</HEAD>
<body >
<form id="frmShowHide" method="post" runat="server">
<asp:TextBox id="txtShowHide"
runat="server"></asp:TextBox>
<asp:CheckBox id="chbShowHide" Checked=True
runat="server"></asp:CheckBox>
</form>
</body>
</HTML>

Open in new window

0
new_to_phpAuthor Commented:
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
0
remorinaCommented:
Try this
There is no need to server side scripting
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

krunal_shahCommented:
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,divName) {
if(boxName.checked = true) {
document.getElementById(divName).style.display = 'block';
}
else {
document.getElementById(divName).style.display = 'none';
}
}

0
remorinaCommented:

<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>

Open in new window

0
new_to_phpAuthor Commented:
remorina,

It works but is there a way to encapsulate the label? Like in a div for instance?
0
remorinaCommented:
it doesn't matter where you put the elements on the page, you can also give the id you want to show/hide to a div instead of an input or label like below
<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 />
<div id="input1" style="border:solid 1px red;display:none;padding:5px;">
<input type="text" />
</div>
</form>
</body>
</html>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
new_to_phpAuthor Commented:
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
0
StealthyDevCommented:
Dear, this would be the simplest way to do that..

  <input type=checkbox checked onclick="txtBox.style.visibility = (this.checked)?'visible':'hidden'">show/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.getElementById('myBoxContainer').style.visibility = (this.checked)?'visible':'hidden'">show/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.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>

Open in new window

0
new_to_phpAuthor Commented:
Appreciate the follow through senthurpandian

I have it figured out now.. thanks to all..
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.