Page 1 of 1

Javascript and Changing Values

Posted: Mon Mar 26, 2007 11:24 pm
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.

Posted: Tue Mar 27, 2007 12:38 am
by Kieran Huggins
make it conditional:

Code: Select all

if(the thing is there){
   remove it
}