How to have two identical js jump menus?
I've got a javascript jump menu on my practice site that I want to show in two places on the same page (one at the top of the page, one at the bottom). Problem is, only the bottom one works, even when I change the name and id attributes. What am I missing?
Here's teh code:
<select name="jump1" id="jump1" class="postform">
<option value="-1">Menu Title</option>
<option value="1">First Post</option>
<option value="2">Second Post</option>
</select>
<script lang="javascript"><!--
var dropdown = document.getElementById("jump1");
function onCatChange() {
if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {
location.href = "http://localhost/?p="+dropdown.options[dropdown.selectedIndex].value;
}
}
dropdown.onchange = onCatChange;
-->
</script>
teammatt3 posted this at 23:50 — 24th March 2008.
He has: 2,102 posts
Joined: Sep 2003
I changed the name of some stuff, and it works for me:
<select name="jump1" id="jump1" class="postform">
<option value="-1">Menu Title</option>
<option value="1">First Post</option>
<option value="2">Second Post</option>
</select>
<script lang="javascript"><!--
var dropdown = document.getElementById("jump1");
function onCatChange() {
if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {
location.href = "http://localhost/?p="+dropdown.options[dropdown.selectedIndex].value;
}
}
dropdown.onchange = onCatChange;
-->
</script>
<select name="jump2" id="jump2" class="postform">
<option value="-1">Menu Title</option>
<option value="1">First Post</option>
<option value="2">Second Post</option>
</select>
<script lang="javascript"><!--
var dropdown2 = document.getElementById("jump2");
function onCatChange() {
if ( dropdown2.options[dropdown2.selectedIndex].value > 0 ) {
location.href = "http://localhost/?p="+dropdown2.options[dropdown2.selectedIndex].value;
}
}
dropdown2.onchange = onCatChange;
-->
</script>
If you use
var
' outside of a function, the variable is still global.Anita.Bonghit posted this at 04:29 — 26th March 2008.
They have: 43 posts
Joined: Sep 2005
Ah, I see the difference now. Working great, thanks TM3!
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.