scriptygoddess

06 Feb, 2007

Javascript set selection in select element

Posted by: Jennifer In: Javascript|jquery|Script snippet

I needed to set the selection of a drop down menu. As far as I can tell, if you don't know the "index" value, then you just have to loop through to set the item as selected. If there's an easier way to do this, please speak up in the comments. I spent WAY too long hunting for a better solution, but this was the only thing that worked:

for (var i=0; i < document.formname.dropdownboxname.length; i++) {
if (document.formname.dropdownboxname[i].value == "value") {
document.formname.dropdownboxname[i].selected = true;
}
}

Update Posting my comment in the main post – because this is the way to do it right: Use jQuery because it rules.

Use this plugin:
http://www.texotela.co.uk/code/jquery/select/

And this code:
$(document).ready(function() {
$(”#selectboxname”).selectOptions(”The Select Value”, true);
});

16 Responses to "Javascript set selection in select element"

1 | Becky

February 6th, 2007 at 6:19 pm

Avatar

If you give the options an id, then you can use that to set the selected value. A co-worker of mine figured out how to go about it, and I've used it myself.

For example, if you have the following:
option 1
option 2
option 3

And you want to select the option with a value of "2", you can do:
document.getElementById("option2").selected = true;

If you name your options in a consistant, predictable manner, then it's not too difficult to do.

I hope that helps (and that I made sense explaining it).

2 | Becky

February 7th, 2007 at 4:39 pm

Avatar

Oops. Looks like some of my comment got eaten.

My example should have been …

[option value="1" id="option1"]option 1[/option]
[option value="2" id="option2"]option 2[/option]
[option value="3" id="option3"]option 3[/option]

3 | Rick

March 1st, 2007 at 8:53 am

Avatar

Thanks to all. This works like a champ. I had several drop-down selects, and gave the options an id in the form of [id of select].[id of option] to keep them straight.

4 | Will

August 27th, 2007 at 7:38 am

Avatar

Was looking for just this script. Thank you for saving me 3 hours.

5 | volomike

March 11th, 2008 at 6:10 pm

Avatar

Was looking for this script. Too bad you don't have ads on this site — I would have clicked on all your ads as a form of thank you.

(You really should sign up with Google AdSense if you're going to post this kind of stuff. I just started last month and with just one blog and without even trying, I managed to get $20. Imagine if I had dozens of blogs, useful info like this, and actually made an effort at it.)

7 | Jennifer

September 6th, 2008 at 10:45 pm

Avatar

Before I go crazy another night trying to find this. Yes, this is much easier with jquery (isn't everything!) 😀

Use this plugin:
http://www.texotela.co.uk/code/jquery/select/

And this code:
$(document).ready(function() {
$("#selectboxname").selectOptions("The Select Value", true);
});

8 | Neuropsykopat

November 2nd, 2008 at 3:23 pm

Avatar

put a break instruction in the if closure. It'll stop the investigations.

for
{
if(found)
{

break;
}
}

9 | TK

December 16th, 2008 at 5:58 pm

Avatar

document.evaluate("//select[@id='selectboxname']//option[@value='The Selected Value', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).selected = true;

10 | Richard

February 11th, 2009 at 7:16 am

Avatar

Just to let you know the selectOptions plugin is not necessary when using JQuery. You can use the following just as well:

$(document).ready(function() {
$("#selectboxname").val("The Select Value");
});

Thanks for the post though, pointed me in the right direction, and yeah JQuery does rule.

12 | David

April 11th, 2009 at 12:30 pm

Avatar

Actually, if your have the id to your select object and you know what the values are, you can just set

selectElement.value = "myValue"

and it will select it properly.

13 | rainabba

July 16th, 2009 at 5:30 pm

Avatar

David: That is freaking hilarious. All those amazingly complex solutions and the answer couldn't be more elegant :)

14 | Jennifer

August 4th, 2009 at 11:58 am

Avatar

@David – except that simply doing selectElement.value = "myValue" doesn't work (at least not in Firefox)… That would be nice, though, if it were that simple. :/

15 | Jennifer

August 4th, 2009 at 12:06 pm

Avatar

@Richard – your solution does work! Good to know I don't need to tack on the selectOptions plugin! Thanks!

16 | Paul

September 14th, 2009 at 9:43 am

Avatar

Here's a generic function that doesn't use jquery in case anyone is looking for one.

function setSelect(id, val) {
elem = document.getElementById(id);
for(i = elem.length – 1; i > 0; i–) {
if(elem[i].value == val) {
elem[i].selected = true;
break;
}
}
}

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements