Javascript and Changing Values

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

Javascript and Changing Values

Post by iknownothing »

Hey Guys,
I have this piece of code that, whan a user selects Other from the drop down a div containing a textbox will appear for the user to type in what "other" is. Currently, selecting "other" works fine, and then selecting another value after selecting "other" also works fine.

Code: Select all

function domainchek() {
	  		var domaincomp = document.getElementById('domaincompany');
	  		var domaintd = document.getElementById('domaintd');
	  		if (domaincomp.value == 'other') {
		  		domaintd.innerHTML+="<div id='domaindivvy' style='margin:0px;padding:0px;'><input type='text' name='domaincompany2' size='22'/></div>";
			}
			else {
  				domaintd.removeChild(domaindivvy);
			}
		}
Problem is, when a value that is not "other" is selected first up, it brings up an error, because "domaindivvy" (see above) never existed. And when i replace..

Code: Select all

domaintd.removeChild(domaindivvy);
...with...

Code: Select all

domaintd.innerHTML="<select><option>etc... without the 'domaindivvy' div";
...I can't select anything other than "other" because it automatically reverts to "Please Select". So, now that you know my life story, is there anyway, it could establish what the last value was (ie. If it was "other" then clear the div, otherwise don't) or is there another way around this that could be used?

If you dont understand what I'm trying to say, this is whats supposed to happen:
User selects a value
If the value is "other" then dispay the textbox
If the value is not other, don't display the textbox

As a user has the option to then change the value:
If the previous value was "other" then clear the textbox
If the previous value was not "other" then dont clear the textbox


Thanks.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

make it conditional:

Code: Select all

if(the thing is there){
   remove it
}
Post Reply