Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Set value of Drop down 1

Status
Not open for further replies.

firsttube

Technical User
Apr 21, 2004
165
CA
I have a <select> drop down menu that I want to set the selected value of using javascript. Can this be done?

Code:
<select name="sel" id="sel">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
And I need to do this without knowing the index of the item.

thanks
ft


Information is not Knowledge, Knowledge is not Wisdom, Wisdom is not Truth, Truth is not Beauty, Beauty is not Love, Love is not Music, Music is the best.
 
This will set the set the value to 3, if the value of 3 doesn't exist in the dropdown it won't do anything.
Code:
document.getElementById("sel").value = 3

-kaht

Looking for a puppy?

silky-icon-left.gif
[small]Silky Terriers are small, relatively odorless dogs that shed no fur and make great indoor pets.[/small]
silky-icon-right.gif
 
that's what I had initially and it does not work.

any other ways to do it?


Information is not Knowledge, Knowledge is not Wisdom, Wisdom is not Truth, Truth is not Beauty, Beauty is not Love, Love is not Music, Music is the best.
 
if you have the value you want to set to, you can do this:

Code:
function setSelectedIndex(s, v) {
    for ( var i = 0; i < s.options.length; i++ ) {
        if ( s.options[i].value == v ) {
            s.options[i].selected = true;
            return;
        }
    }
}

pass in the select object and the value.



*cLFlaVA
----------------------------
[tt]mr. pibb + red vines = crazy delicious![/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
that's what I had initially and it does not work.
After copy/pasting your code and sticking my code in a button (switching the " to ' naturally), it works perfectly in IE6 and Firefox. Copy this into a new html file and try for yourself. If my solution is not working then there is something else that is screwed up.
Code:
<select name="sel" id="sel">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>

<input type="button" value="set to 3" onclick="document.getElementById('sel').value = 3" />

-kaht

Looking for a puppy?

silky-icon-left.gif
[small]Silky Terriers are small, relatively odorless dogs that shed no fur and make great indoor pets.[/small]
silky-icon-right.gif
 
that does not work either. I've checked and checked and the value I am trying to set does exist in the drop down list. Any ideas why this could be?



Information is not Knowledge, Knowledge is not Wisdom, Wisdom is not Truth, Truth is not Beauty, Beauty is not Love, Love is not Music, Music is the best.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top