By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,442 Members | 1,327 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,442 IT Pros & Developers. It's quick & easy.

Changing Value of a Selectbox

P: n/a
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>

Jun 22 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
shankwheat wrote:
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
Jun 22 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.