Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Show/Hide Text Fields Based on User Reponse

Status
Not open for further replies.

byrne1

Programmer
Aug 7, 2001
415
US
Let's say I have a form that has, among other controls, a drop-down box that contains YES and NO in response to a question. If the user selects YES, I want three text boxes to appear below the checkbox. If the user selects NO (default), then I want the text boxes to remain hidden. How can I do this, either in Jscript or VBscript?
 
You will probably need to reload the form if you use VBScript. But you can do everything in JavaScript very easily. Create a <div> element, something like this: <div id=&quot;desc1&quot; style=&quot;VISIBILITY: hidden; POSITION: absolute;&quot;><p>Your text boxes here</p></div> Then you will need to write a JavaScript function that will change the visibility and position of this <div> element depending on the value selected in your text box. Then refer to this function in the onChange event.
 
That sounds great, but being a programmer of very little JavaScript experience, how would I write the Java function?
 
Basically, you source will look like this:
<HTML>
<HEAD>
<TITLE>Title</TITLE>
<script language=&quot;JavaScript&quot;>
function showdiv() {
var dropValue = document.all.select1.value;
var strComment = eval(&quot;document.all.desc1&quot;)
if (dropValue == &quot;Yes&quot;) {
strComment.style.visibility = &quot;visible&quot;;
strComment.style.position = &quot;fixed&quot;;
} else {
strComment.style.visibility = &quot;hidden&quot;;
strComment.style.position = &quot;absolute&quot;;
}
}
</script>
</HEAD>
<BODY>
<p><select name=&quot;select1&quot; onchange=&quot;return showdiv();&quot;>
<option selected value=&quot;No&quot;>No</option>
<option value=&quot;Yes&quot;>Yes</option></select>
</p>
<div id=&quot;desc1&quot; style=&quot;VISIBILITY: hidden; POSITION: absolute;&quot;>
<p><input type=&quot;text&quot; name=&quot;text1&quot;><br>
<input type=&quot;text&quot; name=&quot;text2&quot;>
</p>
</div>
</BODY>
</HTML>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top