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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hiding a form field until another filed value is a certain value

Status
Not open for further replies.

chellweg

IS-IT--Management
Dec 8, 2003
20
US
Is there a quick easy way to hide 2 fields on an HTML form until the value of one of the other fields equals a particular value?
 
Sure... You'd have to put onBlur() events on your form fields, then run JavaScript to hide/show the other form field. Something like this:

Code:
<HTML>
<HEAD>

<SCRIPT LANGUAGE=&quot;JavaScript&quot;>

function checkFormValues()
{
	var f1Value = document.getElementById('field1').value;
	var f2Value = document.getElementById('field2').value;
	var f3 = document.getElementById('field3');

	if (f1Value == '1' && f2Value == '2')
		f3.style.visibility = 'visible';
	else
		f3.style.visibility = 'hidden';
}

</SCRIPT>
</HEAD>
<BODY>

<FORM>
	Field 1: <INPUT TYPE=&quot;text&quot; ID=&quot;field1&quot; onBlur=&quot;checkFormValues();&quot;><BR>
	Field 2: <INPUT TYPE=&quot;text&quot; ID=&quot;field2&quot; onBlur=&quot;checkFormValues();&quot;><BR>
	Field 3: <INPUT TYPE=&quot;text&quot; ID=&quot;field3&quot; STYLE=&quot;visibility:hidden;&quot;><BR>
</FORM>

</BODY>
</HTML>

If you enter &quot;1&quot; on box 1, and &quot;2&quot; in box 2, box 3 shows up.. Otherwise box 3 remains hidden.

Hope this helps!

Dan
 
Try this:


<html>
<head>
<title> New Document </title>

<script language=&quot;javascript&quot;>

function test()
{
if(document.myform.text1.value==&quot;test&quot;)
{
document.myform.text2.style.display='block';
}
}

</script>

</head>

<body>

<form name=&quot;myform&quot;>
<input type=&quot;text&quot; name=&quot;text1&quot; onblur=&quot;javascript:test()&quot;>
<input type=&quot;text&quot; name=&quot;text2&quot; style=&quot;display:none&quot;>
<input type=&quot;submit&quot;>
</form>

</body>
</html>



J~
 
Dan, this works great with one exception... The title of the field is not hidden. Is there an easy way to hid that as well?
 
I have one piece of advice for using the above code: Be sure that your page works in NS and IE.

For instance, the
Code:
display
property cannot be changed at runtime (after the page has loaded) in NS, but it can in IE. So chappi's suggestion might work in IE, but it probably won't in NS. I'm not sure if the
Code:
visibility
property can be changed at runtime. You'll need to test it out.

Good luck,
-Ron

-We all play from the same deck of cards, it's how we play the hand we are dealt which makes us who we are.
 
To hide the title and the form field, place both items in a
Code:
<span></span>
tag and hide the span instead of the individual field:

Code:
<HTML>
<HEAD>

<SCRIPT LANGUAGE=&quot;JavaScript&quot;>

function checkFormValues()
{
    var f1Value = document.getElementById('field1').value;
    var f2Value = document.getElementById('field2').value;
    var f3 = document.getElementById('field3');

    if (f1Value == '1' && f2Value == '2')
        f3.style.visibility = 'visible';
    else
        f3.style.visibility = 'hidden';
}

</SCRIPT>
</HEAD>
<BODY>

<FORM>
    Field 1: <INPUT TYPE=&quot;text&quot; ID=&quot;field1&quot; onBlur=&quot;checkFormValues();&quot;><BR>
    Field 2: <INPUT TYPE=&quot;text&quot; ID=&quot;field2&quot; onBlur=&quot;checkFormValues();&quot;><BR>
Code:
    <span id=&quot;field3&quot; style=&quot;visibility:hidden;&quot;>Field 3: <input type=&quot;text&quot;><br /></span>
Code:
</FORM>

</BODY>
</HTML>

Note: I moved the
Code:
id=&quot;field3&quot;
and
Code:
style=&quot;visibility:hidden;&quot;
attributes from the field to the span tag. Also, all tags and attributes need to be lowercase for your page to be XHTML compliant. I made the appropriate changes to the line that I altered.

Hope that does what you want.
-Ron

-We all play from the same deck of cards, it's how we play the hand we are dealt which makes us who we are.
 
Here is my updated code with the form fields I need, but it does not seem to work now... I am guessing I am using the wrong operator for &quot;OR&quot;


<HTML>
<HEAD>

<SCRIPT LANGUAGE=&quot;JavaScript&quot;>

function checkFormValues()
{
var f1Value = document.getElementById('urgency').value;
var f2 = document.getElementById('phone').value;
var f3 = document.getElementById('timetocall');

if (f1Value == 'We will be needing help in the near future.' | We need help immediately!)
f2.style.visibility = 'visible';
f3.style.visibility = 'visible';
else
f2.style.visibility = 'hidden';
f3.style.visibility = 'hidden';
}

</SCRIPT>
</HEAD>
<BODY>

<FORM>
<p><select size=&quot;1&quot; name=&quot;urgency&quot;>
<option>We might be needing help in the near future.</option>
<option>We will be needing help in the near future.</option>
<option>We need help immediately!</option>
</select><br>
<span id=&quot;timetocall&quot; style=&quot;visibility:hidden;&quot;>Best time to call: <input type=&quot;text&quot;><br /></span>
<span id=&quot;phone&quot; style=&quot;visibility:hidden;&quot;>Phone Number: <input type=&quot;text&quot;><br /></span>

</p>
</FORM>

</BODY>
</HTML>
 
There are two &quot;||&quot; (pipes) in an OR. Try this:

Code:
<HTML>
<HEAD>

<SCRIPT LANGUAGE=&quot;JavaScript&quot;>

function checkFormValues()
{
    var f1Value = document.getElementById('urgency').value;
    var f2 = document.getElementById('phone').value;
    var f3 = document.getElementById('timetocall');

    if (
Code:
f1Value == 'We will be needing help in the near future.' || f1Value == 'We need help immediately!')
Code:
        f2.style.visibility = 'visible';
        f3.style.visibility = 'visible';
    else
        f2.style.visibility = 'hidden';
        f3.style.visibility = 'hidden';
}

</SCRIPT>
</HEAD>
<BODY>

<FORM>
    <p><select size=&quot;1&quot; name=&quot;urgency&quot;>
                      <option>We might be needing help in the near future.</option>
                      <option>We will be needing help in the near future.</option>
                      <option>We need help immediately!</option>
                    </select><br>
        <span id=&quot;timetocall&quot; style=&quot;visibility:hidden;&quot;>Best time to call: <input type=&quot;text&quot;><br /></span>
        <span id=&quot;phone&quot; style=&quot;visibility:hidden;&quot;>Phone Number: <input type=&quot;text&quot;><br /></span>

    </p>
</FORM>

</BODY>
</HTML>

-Ron

-We all play from the same deck of cards, it's how we play the hand we are dealt which makes us who we are.
 
No Go.. Code Changed to this:

<HTML>
<HEAD>

<SCRIPT LANGUAGE=&quot;JavaScript&quot;>

function checkFormValues()
{
var f1Value = document.getElementById('urgency').value;
var f2 = document.getElementById('phone').value;
var f3 = document.getElementById('timetocall');

if (f1Value == 'We will be needing help in the near future.') || f1Value == ('We need help immediately!')
f2.style.visibility = 'visible';
f3.style.visibility = 'visible';
else
f2.style.visibility = 'hidden';
f3.style.visibility = 'hidden';
}

</SCRIPT>
</HEAD>
<BODY>

<FORM>
<p><select size=&quot;1&quot; name=&quot;urgency&quot; onBlur=&quot;checkFormValues();&quot;>
<option>We might be needing help in the near future.</option>
<option>We will be needing help in the near future.</option>
<option>We need help immediately!</option>
</select><br>
<span id=&quot;timetocall&quot; style=&quot;visibility:hidden;&quot;>Best time to call: <input type=&quot;text&quot;><br /></span>
<span id=&quot;phone&quot; style=&quot;visibility:hidden;&quot;>Phone Number: <input type=&quot;text&quot;><br /></span>

</p>
</FORM>

</BODY>
</HTML>
 
You can't close the parenthesis of the
Code:
if
statement and then add another condition. Also, you're missing some brackets:

Code:
function checkFormValues()
{
    var f1Value = document.getElementById('urgency').value;
    var f2 = document.getElementById('phone').value;
    var f3 = document.getElementById('timetocall');

    if
Code:
(f1Value == 'We will be needing help in the near future.' || f1Value == 'We need help immediately!'){
Code:
        f2.style.visibility = 'visible';
        f3.style.visibility = 'visible';
Code:
}
Code:
else
Code:
{
Code:
        f2.style.visibility = 'hidden';
        f3.style.visibility = 'hidden';
}

-Ron

-We all play from the same deck of cards, it's how we play the hand we are dealt which makes us who we are.
 
The code compiles correctly now, but when you change the drop down, it produces a style error. Current code:

<HTML>
<HEAD>

<SCRIPT LANGUAGE=&quot;JavaScript&quot;>

function checkFormValues()
{
var f1Value = document.getElementById('urgency').value;
var f2 = document.getElementById('phone').value;
var f3 = document.getElementById('timetocall');

if (f1Value == 'We will be needing help in the near future.' || f1Value == 'We need help immediately!')
{ f2.style.visibility = 'visible';
f3.style.visibility = 'visible';}
else{
f2.style.visibility = 'hidden';
f3.style.visibility = 'hidden';}
}


</SCRIPT>
</HEAD>
<BODY>

<FORM>
<p><select size=&quot;1&quot; name=&quot;urgency&quot; onBlur=&quot;checkFormValues();&quot;>
<option>We might be needing help in the near future.</option>
<option>We will be needing help in the near future.</option>
<option>We need help immediately!</option>
</select><br>
<span id=&quot;timetocall&quot; style=&quot;visibility:hidden;&quot;>Best time to call: <input type=&quot;text&quot;><br /></span>
<span id=&quot;phone&quot; style=&quot;visibility:hidden;&quot;>Phone Number: <input type=&quot;text&quot;><br /></span>

</p>
</FORM>

</BODY>
</HTML>
 
Okay, here you go...

You must assign a value to each &quot;option&quot; in the drop down box. That is the value you will be referencing in the javascript, not the text which is displayed.

I made a few different changes this time, so I won't be referencing my changes in a different color like before.

Code:
<HTML>
<HEAD>

<SCRIPT LANGUAGE=&quot;JavaScript&quot;>

function checkFormValues()
{
    var f1Value = document.getElementById('urgency').value;
    var f2 = document.getElementById('phone');
    var f3 = document.getElementById('timetocall');

    if (f1Value == &quot;2&quot; || f1Value == &quot;3&quot;){
        f2.style.visibility = &quot;visible&quot;;
        f3.style.visibility = &quot;visible&quot;;
		}else{
        f2.style.visibility = &quot;hidden&quot;;
        f3.style.visibility = &quot;hidden&quot;;
		}
}


</SCRIPT>
</HEAD>
<BODY>

<FORM>
    <p><select size=&quot;1&quot; name=&quot;urgency&quot; onChange=&quot;checkFormValues();&quot;>
                      <option value=&quot;1&quot;>We might be needing help in the near future.</option>
                      <option value=&quot;2&quot;>We will be needing help in the near future.</option>
                      <option value=&quot;3&quot;>We need help immediately!</option>
                    </select><br>
        <span id=&quot;timetocall&quot; style=&quot;visibility:hidden;&quot;>Best time to call: <input type=&quot;text&quot;><br /></span>
        <span id=&quot;phone&quot; style=&quot;visibility:hidden;&quot;>Phone Number: <input type=&quot;text&quot;><br /></span>

    </p>
</FORM>

</BODY>
</HTML>

I have two pieces of advice:
1. When you post code to tek-tips, enclose the code in
Code:
[code]
[/code] tags to make it more readable.

2. Refine your javascript to work in more browsers. Different versions of IE & NS reference the Document Object Model (DOM) in different ways. The above code will not work in all browsers. I found a good example of code that will work here (
Good luck,
-Ron


-We all play from the same deck of cards, it's how we play the hand we are dealt which makes us who we are.
 
The sample code in this FAQ works on IE, Netscape and Opera:
faq215-4585
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top