How to set select options in jquery
Thanks for the comment,. Keep in mind the. If you would like to select a value based on the item's value and not the index of that item then you can do the following:. What's important to understand is that val for a select element returns the value of the selected option, but not the number of element as does selectedIndex in javascript. It's not all that efficient.
The attribute "selected" is not how you specify a selected radio button in Firefox and Chrome at least. Use the "checked" attribute:. The pure javascript selectedIndex attribute is the right way to go because,it's pure javascript and works cross-browser:. Here is a jsfiddle demo with two dropdowns using one to set the other:.
javascript - Adding options to a
You can also call this before changing the selectedIndex if what you want is the "selected" attribute on the option tag here is the fiddle:. Where name is select id and idx is the option value you want selected. Select the item based on the value in the select list especially if the option values have a space or weird character in it by simply doing this:.
Also, if you have a dropdown as opposed to a multi-select you may want to do a break; so you don't get the first-value-found to be overwritten.
I need a solution that has no hard coded values in the js file; using selectedIndex. Most of the given solutions failed one browser. This appears to work in FF10 and IE8 can someone else test in other versions. Now I don't care about the index I just wanted the item by its value. I faced same problem.
First you need go through the events i. The Second event is selecting default option using any function such as val etc. You should ensure that the Second event should happen after the First event.
To achieve this take two functions lets say generateSelectbox for genrating select box and selectDefaultOption. You need to ensure that selectDefaultOption should be called only after the execution of generateSelectbox. You can try following code. 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 an select box: For example, if I am trying to set "Number 3", I'm trying this: How can I set an option as selected based on it's index using jQuery?
How to Use Bootstrap Select Plugin with Ajax Jquery PHPMarc 8, 2 21 The chosen answer by John Kugelman did not work for me in IE7. Mackey Jun 27 '11 at Sadly, none of these ways fire a change event for me. When you're programmatically setting a selected option, you may not always want the bound change event to be triggered like on a page load event.
Add a new option to a select with jQuery - Electric Toolbox
To solve that jquery leaves it up to the developer to decide. Is the first option 0 or 1? I thought it worth mentioning that post-jQuery 1.
How To Set Drop Down Value using JQUery
This may also be useful, so I thought I'd add it here. If you would like to select a value based on the item's value and not the index of that item then you can do the following: Just be aware that.
If you need to change the selected value more often e. Chris Brandsma 9, 5 35 This is simple and straightforward. I much prefer this solution. This works in Chrome, but doesn't work for me with IE9. FWIW, This doesn't even work in Chrome for me. This worked for me, I also needed to select by INDEX not VALUE. This answer gave me what i needed.. Mb Rostami 2, 19 To deselect the option use an empty array: Anonymous 1, 4 28 NicolasBernier 1, 11 To clarify Marc's and John Kugelman's answers, you could use: You don't actually need the jQuery object though.
This will do the trick: Use the "checked" attribute: Spycho 4, 2 20 The pure javascript selectedIndex attribute is the right way to go because,it's pure javascript and works cross-browser: Drew 1 8 I've always had issues with prop 'selected' , the following has always worked for me: Object has no method 'attr'" This syntax stops those errors. Select the item based on the value in the select list especially if the option values have a space or weird character in it by simply doing this: Nexxas 5 The First event is generating select box with options.
To achieve this take two functions lets say generateSelectbox for genrating select box and selectDefaultOption You need to ensure that selectDefaultOption should be called only after the execution of generateSelectbox. You can also init multiple values if your selectbox is a multipl: You can try following code code: Ram Lakhan Yadav 33 3.
How to dynamically select option in dropdown menu? - jQuery Forum
Stack Overflow works best with JavaScript enabled. This is almost exactly the same as you previous question: This makes copying from one element to another dead simple as well! To achieve this take two functions lets say generateSelectbox for genrating select box and selectDefaultOption You need to ensure that selectDefaultOption should be called only after the execution of generateSelectbox share improve this answer.
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.