Changing Value of a Selectbox

S

shankwheat

I've been using this little script to change the <a href=""></a> value
of a link which works well.

<script language="javascript" type="text/javascript">
function ordering(sorder)
{
var link = document.getElementById("mylink").href =
"http://www.mysite.com?Option="+sorder;
}
</script>

<a id="mylink" href="http://www.mysite.com/">Create PDF</a>


I'm wondering how I would do the same thing to change the value in a
selectbox like this?

<select size="1" name="menu">
<option value="http://www.mysite.com?Option=reports">Reports</option>
<option value="http://www.mysite.com?Option=pdf">Create PDF</option>
<option value="http://www.mysite.com?Option=bylaws">Bylaws</option>
</select>
 
J

Jeremy

shankwheat said:
I'm wondering how I would do the same thing to change the value in a
selectbox like this?

<select size="1" name="menu">
<option value="http://www.mysite.com?Option=reports">Reports</option>
<option value="http://www.mysite.com?Option=pdf">Create PDF</option>
<option value="http://www.mysite.com?Option=bylaws">Bylaws</option>
</select>

Well, first you're going to have to know *which* value you want to
change - in your example, there's three. On which do you want to operate?

You can get a collection of the option elements like this:

HTML:
<select size="1" name="menu" id="menu">
<option value="...">...</option>
...
</select>

Script:
var menu = document.getElementById("menu");
var options = menu.getElementsByTagName("option");

If you want to operate on whichever element is selected, try this:

options[menu.selectedIndex].value = "a new value";

Otherwise just pick the option you want by indexing it into the
collection, and change its value attribute to what you want.

Jeremy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top