Jump to content

[SOLVED] document.getElementById(" ") is null issue.


Recommended Posts



I want the contents of a text box to change with the selection from a dropdown menu.  The script below works, but only if I include the line marked <!--why do I need this-->.  If I delete this line the page loads without error but I get document.getElementById("divSCO0") is null when I make a selection and no changing text.


How can I get the script to work without the <!--why do I need this--> line?


Can I get divXXX not to display? I've got style="display:none" but it still shows!


The number of select options and divs  will vary with database content.  Could the java script be written before I have the results of the database query? I guess I'd have to pass it both the total number of options and which option to display?


Thanks for your time.


<script type="text/javascript">
function checker(what){
	document.getElementById("divSCO0").style.display = "none";
	document.getElementById("divSCO1").style.display = "none";
	document.getElementById("divSCO2").style.display = "none";
if (what==1) {
	document.getElementById("divSCO1").style.display = "block";
}else if (what==2) {
	document.getElementById("divSCO2").style.display = "block";
}else {
	document.getElementById("divSCO0").style.display = "block";


<select name="DDselect" onchange="checker(this.value)"><option value="0" selected></option>
<option value="1">1</option>
<option value="2">2</option>


<div id="divXXX" style="display:none"><textarea name="XXX" id="XXX" rows="0" cols="0"></textarea></div><!--why do I need this-->
<div id="divSCO0" style="display:block"><textarea name="SCO0" id="SCO0" rows="2" cols="30"></textarea></div>
<div id="divSCO1" style="display:none"><textarea name="SCO1" id="SCO1" rows="2" cols="30">Selection one from the dropdown</textarea></div>
<div id="divSCO2" style="display:none"><textarea name="SCO2" id="SCO2" rows="2" cols="30">Two selected</textarea></div>


Link to comment
Share on other sites



I can get rid of the unwanted text box by changing the line to...

<div id="divXXX" style="display:none"><input type=hidden></div><!--why do I need this-->


But if you know the 'proper' solution to the problem I'd be grateful for the insight.



Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.