NameSilo

Show and hide a textfield based on a selectbox

Spaceship Spaceship
Watch

asgsoft

VIP Member
Impact
9
I am using this JS to show and hide a textbox when the value for the select box changes.

It shows the textbox alright, but when I want to hide it it won't.

Here is what I have:

Code:
function showField(email_div)
{
 var txt = "";
 if(document.form1.type.value = "Banner Impressions")
 {
   txt += "<p>Banner Location:<input type='text' name='bannerloc' value='<?php echo $firstname; ?>' onfocus=\"this.value='';\" /></p>";
 document.getElementById(email_div).innerHTML = txt;
 }
 else
 {
 txt = null;
  document.getElementById(email_div).innerHTML = nill;
 }

}

Where have I gone wrong?
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
.US domains.US domains
Hi asgsoft. Can i assume email_div is the layer you wish to hide?

Technically you are not hiding anything here, in affect you are you are removing it. Remember that is is null not nill btw ;)

PHP:
function showField(email_div)
{
	if(document.form1.type.value = "Banner Impressions")
	{
  		var txt = '<p>Banner Location:<input type="text" name="bannerloc" value="<?php echo $firstname; ?>" onfocus="this.value=\'\';" /></p>';
		document.getElementById(email_div).innerHTML = txt;
 	}
 	else
 	{
  		document.getElementById(email_div).innerHTML = null;
  		/* 	This line would actually *hide* the div instead of erasing the contents 
  		@	document.getElementById(email_div).style.display = 'none';
  		*/
 	}
}

Try that. If not can your provide an example of how you are using this please.
 
0
•••
It still doesn't hide the div.
 
0
•••
Matthew. said:
Try that. If not can your provide an example of how you are using this please.

:blink:
 
0
•••
Code:
function showField(email_div) 
{ 
    if(document.form1.type.value = "Banner Impressions") 
    { 
          var txt = '<p>Banner Location:<input type="text" name="bannerloc" value="<?php echo $firstname; ?>" onfocus="this.value=\'\';" /></p>'; 
        document.getElementById(email_div).innerHTML = txt; 
        document.getElementById(email_div).style.display = 'block';
     } 
     else 
     { 
          document.getElementById(email_div).style.display = 'none';
     } 
}

If the value is Banner impressions it will fill email_div with the txt and make the div display and show. If it isn't then the div will not display and will be hidden.
 
0
•••
0
•••
Right that helped :)

Look at your option again:
Code:
<option value="banner">Banner Impressions</option>

See the value is actually banner not Banner Impressions, so we change this line:
Code:
if(document.form1.type.value = "Banner Impressions")
To:
Code:
if(document.form1.type.value == "banner")

(also note the == which we both missed)

PHP:
function showField(email_div) 
{ 
    if(document.form1.type.value == "banner") 
    { 
          var txt = '<p>Banner Location:<input type="text" name="bannerloc" value="" onfocus="this.value=\'\';" /></p>'; 
        document.getElementById(email_div).innerHTML = txt; 
        document.getElementById(email_div).style.display = 'block';
     } 
     else 
     { 
          document.getElementById(email_div).style.display = 'none';
     } 
}
 
1
•••
thanks a lot. That's fixed now.

Can you have a look at the price section?

http://asgsoft.net/form/form.php

It should make the price change, it only works when i have one price set, when I have all of them then it doesn't work.


Where have i gone wrong?
 
Last edited:
0
•••
Dynadot — .com Registration $8.99Dynadot — .com Registration $8.99

We're social

Unstoppable Domains
Domain Recover
DomainEasy — Live Options
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back