MyBB Community Forums

Full Version: JavaScript help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Alright I need some help with JavaScript.

So say I have the following drop down:

<select id="dropdown">
<option value="http://google.com">Google</option>
<option value="http://wikipedia.com">Wikipedia</option>
...
</select>

And I also have a link:
<a target="_blank" href="#">Go</a>

How can I get the link's location to change whenever the user selects a value from the drop down box? (eg if the user selects Google the link goes to http://google.com)
Take a look at the code that powers the forum jump menu.
Try this...

Javascript:
<script type="text/javascript">
function MM_jumpMenu(targ,selObj,restore){ //v3.0
	eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
	if (restore) selObj.selectedIndex=0;
}
</script>

HTML:
<form action="" method="POST">
	<select id="dropdown" onchange="MM_jumpMenu('parent',this,0)">
		<option value="http://google.com">Google</option>
		<option value="http://wikipedia.com">Wikipedia</option>
	</select>
</form>
Doesn't open in a new tab though, and I don't want it to open as you select it.

I came up with a solution that works though after some tinkering:

function update_url()
{
    document.getElementById('link_id').href = document.getElementById('dropdown').value;
}

<select id="dropdown" onchange="update_url()">
<option value="http://google.com">Google</option>
<option value="http://wikipedia.com">Wikipedia</option>
...
</select>

<a id="link_id" target="_blank" href="#">Go</a>

Thanks for trying to help, though.