Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

13
  • 24
    This is wrong because you are SETTING the value of ALL the SELECT elements to the_value. .val is not selector/filter function! It is a property accessor and passing it that string SETS the value. I tried to take my upvote back but was too late after I reallized this in testing. Commented May 26, 2011 at 21:14
  • 13
    Please @AaronLS and @guilhermeGeek, refer to the docs (last example). It works as a selector for select, checkboxes and radio buttons, and as a value setter for text inputs and textareas. Commented May 27, 2011 at 15:46
  • 9
    You have misread the documentation. For checkboxes, radios, and listboxes the command sets the "selected" attribute for those items. The last example shows how passing val("checkbox 1") causes it to become selected. This is not the same as a "selector" in terms of getting the objects via CSS/jquery selector. I tested your code, it doesn't work bottom line. Commented May 31, 2011 at 14:11
  • 31
    +1: no, it's not a JQuery selector, however I think most people searching for this question are like me, and simply want to change the currently selected option; coming up with an actual selector is merely a way to do that. And this is certainly better than answers that I've seen that have you iterate over all of the options. Commented Aug 4, 2012 at 13:44
  • 5
    @theUtherSide: I would not always go by a consensus. The val() setter on a select is implemented by setting the selected property of a child option where the value matches, so is really does do what the question wanted (but only for value searches and not text searches). Commented Sep 6, 2016 at 11:24