Jump to content

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


ann

Recommended Posts

Hi,

 

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?

or

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.

 

<html>
<head>
<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";
}
} 
</script>
</head>

<body>

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

<br><br>

<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>

</body>
</html>

Link to comment
Share on other sites

Update

 

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.

 

Cheers

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

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

Guest
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.