Jquery set select value based on text

Jquery set select value based on text

Author: rocknroll Date: 01.07.2017

I have been using the following code with jQuery v1. This code worked fine, until I noticed one select list on which it failed, depending on the text that was being matched.

After some hair-pulling I realized that it was failing only in cases where the text was a single word no spaces, no non-alpha characters. All of my previous uses of this code had worked fine with select lists comprised solely of multi-word chemical names. For example, within the one select list, it worked fine with: I have tried any way I could think of to surround the text variable with quotes both single and double to no avail.

But why should a single word need quotes when a two word phrase does not? It's worth noting that quotes are acceptable when using the 'value' attribute.

javascript - jQuery: Setting select list 'selected' based on text, failing strangely - Stack Overflow

Below is some code to demonstrate the problem. Two select lists, the first of which is comprised of simple words, the second of two word phrases. When the page loads it tries to set the value of each select list. The jQuery code works for the second list but not the first. I tried putting a space in 'monkey' to get 'mon key' and it worked! A working demo of the code below is here. I would greatly appreciate any insight into what I am doing wrong here. Or even an alternative selector syntax for using the 'text' attribute.

Thanks in advance to all who have the time to assist.

You can test that version here. Using the filter function seems to work in your test cases tested in Firefox. The selector would look like this:. In case someone google for this, the solutions above didn't work for me so i ended using "pure" javascript. And that would set the value and make the current value selected in the combo box.

We can do it by searching the text in options of dropdown list and then by setting selected attribute to true. I tried a few of these things until I got one to work in both Firefox and IE. This is what I came up with. By posting your answer, you agree to the privacy policy and terms of service.

By subscribing, you agree to the privacy policy and terms of service. Stack Overflow Questions Developer Jobs Documentation beta Tags Users.

jQuery set dropdown option based on text | [H]ard|Forum

Sign up or log in to customize your list. Tour Start here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us.

Log In Sign Up. Join the Stack Overflow Community.

jquery set select value based on text

Stack Overflow is a community of 7. Join them; it only takes a minute: Setting select list 'selected' based on text, failing strangely Ask Question. I have tried hard coding the text in there and had the same result. This did not work: Snubian 1 4 8. Is there a reason you cannot simply set the selected index of the list? Selecting based on the text value isn't really the best way to do this. I get your point, but I don't know the index value at the time.

Basically I build a table of data from a database, the user can edit a row by clicking on it and a number of select lists below are filled with the values from the table this is what I use the code above for. The select list index values are id values from the database tables.

In theory I could store these invisibly in the table along with the text descriptions but I am trying to cut down on overhead where possible.

Thanks so much for your quick response. Just what I was after. I do in fact have values set in the actual code. I'd still be very much interested if anyone has any idea why my original code seemed to fail as it did in particular circumstances Snubian - was the casing and such correct? I'm positive that the casing was fine, the code was failing only in very specific circumstances as I've tried to describe.

And I've tested with hard-coding values as described above. The failure of the "Monkey" example in my test code seems to indicate that this usage fails in certain cases. Running the code on jsFiddle, you can see if you simply put a space in "Mon key", both the text1 variable and in the list option, it suddenly works. Modern readers should note that this answer is out of date; using. Thanks, appreciate it very much. Still new to jQuery so every little but helps. Deivide 1 9.

The selector would look like this: Thanks, this indeed seems to work, similar approach below also. In case someone google for this, the solutions above didn't work for me so i ended using "pure" javascript document.

jquery set select value based on text

The following works for text entries both with and without spaces: This code is run in every environment. Kaushik Das 69 1 Where did you get that roll thing??

jquery set select value based on text

This answer does not seem to differ in any appreciable way from the other answers, including the accepted answer, which is much more succinct. Sign up or log in StackExchange. Sign up using Facebook.

javascript - jQuery - setting the selected value of a select control via its text description - Stack Overflow

Sign up using Email and Password. Post as a guest Name. In it, you'll get: The week's top questions and answers Important community announcements Questions that need answers. Stack Overflow works best with JavaScript enabled. MathOverflow Mathematics Cross Validated stats Theoretical Computer Science Physics Chemistry Biology Computer Science Philosophy more 3.

Meta Stack Exchange Stack Apps Area 51 Stack Overflow Talent.

inserted by FC2 system