In the above script I’m only able to change one div… What I want is to be able to, if a user changes the selectbox from “# 1” to “# 2”, to change the viewable div… Hope this makes sense and somebody can help…
The major error here is that the ID attribute is unique. So you have to change them so they are different. You’re also going about this the wrong way. I’d do this:
document.getElementsByName('matchplaytype')[0].onchange = toggleOptions;
function toggleOptions() {
if (this.value == '0') return false;
var objdiv = document.getElementById('options' + this.value);
objdiv.style.display = obj.style.display = 'none' ? '' : 'none';
}
<select name="matchplaytype">
<option value="0">Choose</option>
<option value="1"># 1</option>
<option value="2"># 2</option>
</select>
<div id="options1"><p>This is #1</p></div>
<div id="options2"><p>This is #1</p></div>
The javascript would need to run after the HTML has loaded. This can be achieved by putting the JS just before the closing </body> tag.
Note the following:
No need for the onchange inline event handler in <select> (it’s a dirty way of doing things)
Your id names are horrible. Far too long and unreadable. Keep it simple, you’re less likely to make silly mistakes that way
Don’t use the language attribute in <script>. Use type=“text/javascript”
Don’t use <table> to lay this out. That is an extremely outdated method, unless of course you’re actually going to have a proper table with tabular data in there.
I tryied it your way, but nothing seems to happen? The options1 and 2 div a both viewable from the start, and should not be, and if I change the select box nothing happens??? Any idea?