How to have two identical js jump menus?

They have: 43 posts

Joined: Sep 2005

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>

&lt;script lang="javascript"&gt;<!--
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;
-->
&lt;/script&gt;

teammatt3's picture

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>

&lt;script lang="javascript"&gt;<!--
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;
-->
&lt;/script&gt;


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

&lt;script lang="javascript"&gt;<!--
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;
-->
&lt;/script&gt;
'

If you use var' outside of a function, the variable is still global.

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.