Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Donate Today!

Do you enjoy these
technical forums?
Donate Today! Click Here

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

firsttube (TechnicalUser) (OP)
15 Aug 06 15:06
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.

Helpful Member!  kaht (Programmer)
15 Aug 06 15:08
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 Terriers are small, relatively odorless dogs that shed no fur and make great indoor pets.

firsttube (TechnicalUser) (OP)
15 Aug 06 15:10
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.

cLFlaVA (Programmer)
15 Aug 06 15:26
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
----------------------------
mr. pibb + red vines = crazy delicious!

http://www.coryarthus.com/

kaht (Programmer)
15 Aug 06 15:39

Quote:

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 Terriers are small, relatively odorless dogs that shed no fur and make great indoor pets.

firsttube (TechnicalUser) (OP)
15 Aug 06 16:00
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.

cLFlaVA (Programmer)
15 Aug 06 16:01
yeah, your other code.


 
*cLFlaVA
----------------------------
mr. pibb + red vines = crazy delicious!

http://www.coryarthus.com/

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close