How to set select option using jquery

By: antimaul Date of post: 10.06.2017

Can't set the selected option for dropdown using JQuery

Using jQuery I want to set the selected element of the select control to be the item with the text description I have as opposed to the value, which I don't have. But I'm a bit stumped on doing it via the text description.

I guess there must be a way of getting the value out from the text description, but my brain is too Friday afternoon-ed to be able to work it out. Select by description for jQuery versions below 1.

Note that while this approach will work in versions that are above 1. It will not work in jQuery 1. Tested this and works. To avoid all jQuery version complications, I honestly recommend using one of these really simple javascript functions I know this is an old post, but I couldn't get it to select by text using jQuery 1.

I ended up using the following code variation of spoulson's solution:. You can also clear already selected options: Just on a side note.

javascript - Use jQuery to set select box value to first option - Stack Overflow

My selected value was not being set. And i had search all over the net. Actually i had to select a value after a call back from a web service, because i was getting data from it.

jquery - Set select option 'selected', by value - Stack Overflow

I found that by using attr you would end up with multiple options selected when you didn't want to - solution is to use prop:. I had a problem with the examples above, and the problem was caused by the fact that my select box values are prefilled with fixed length strings of 6 characters, but the parameter being passed in wasn't fixed length.

Set HTML Select Options Selected value Using jQuery

I have an rpad function which will right pad a string, to the length specified, and with the specified character. So, after padding the parameter it works. Get the children of the select box; loop through them; when you have found the one you want, set it as the selected option; return false to stop looping. This accepted answer does not seem correct, while.

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site the association bonus does not count. Would you like to answer one of these unanswered questions instead? Stack Overflow Questions Developer Jobs Documentation beta Tags Users. 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. Stack Overflow is a community of 7. Join them; it only takes a minute: I have a select control, and in a javascript variable I have a text string. I know setting it by value is pretty trivial. Dan Atkinson 8, 10 66 Select by description for previous versions: Are you not seeing it?

Note that jQuery 1. So, what's the best way to do it in 1. In more recent versions of jQuery,. The first answer to this question appears to be the solution post 1.

Dec 20 '11 at 0: I haven't tested this, but this might work for you. Works great in 1. Josh Crozier k 27 I tried to do it without the. In many cases, this approach doesn't work unfortunately.

This method doesnt seem to work for 1. Make sure that you are first removing the selected attribute before re-setting, otherwise it will appear not to work works for me in 1.

Google javascript dom properties vs attributes if you don't know the distinction. I do it on this way jQuery 1.

Jakub Matczak 9, 1 26 ErazerBrecht 9 Thanks - I think this might be the version I use, as I also need to have logic for when there is no matching text, and this seems the easiest mechanism, for being able to do that. Dave 11 How does this help at all? It enables the selection of an select option via value or text depending on function called.

how to set select option using jquery

I ended up using the following code variation of spoulson's solution: Losbear 1, 18 No, that's not what I meant. I think my comment was a bit unclear, so I just deleted it. I did upvote your answer though, because it worked for my situation. Easiest way with 1. Khatri 2, 5 27 Diego Unanue 2, 2 18 Kimtho6 4, 7 30 Vitor Silva 6, 7 26 Ammar Bukhari 2 This is the right one - altough Should work as is. I found that by using attr you would end up with multiple options selected when you didn't want to - solution is to use prop: Chris Edwards 1, 1 15 Keith 2 5 Sam Alexander 1 Can you post some example code?

Heres an easy option. Just set your list option then set its text as selected value: Misa Lazovic 2, 9 21 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. DanAtkinson was about to do the same myself. This feels like it shouldn't work it appears it could be ambiguous but it actually should work fine for me.

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.

how to set select option using jquery
inserted by FC2 system