If you don't mind modifying your HTML a little to include the value attribute of the options, you can significantly reduce the code necessary to do this:.
If you decide not to include the use of the value attribute, you will be required to cycle through each option, and manually check its value:.
I'd iterate through the options, comparing the text to what I want to be selected, then set the selected attribute on that option. Once you find the correct one, terminate the iteration unless you have a multiselect. You can follow the.
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.
.change() | jQuery API Documentation
Join the Stack Overflow Community. Stack Overflow is a community of 7.Adding attributes to an element using Jquery
Join them; it only takes a minute: JQuery setting the selected attribute on a select list Ask Question. Rupert 5 19 If you don't mind modifying your HTML a little to include the value attribute of the options, you can significantly reduce the code necessary to do this: Sampson k 51 If you want to fire the.
This answer predates that by a few years. Additionally, though not my intention, by changing the attribute you can enable greater control over styling via attribute selectors. Darren Rose 99 1 1. If you are using JQuery, since the 1.
Is there a reason to use. I think the difference is mainly stylistic.
Under the hood I would expect them to operate basically the same way. Get a collection, filter it.
jquery - Adding "selected" attribute to the selected option in PHP - Stack Overflow
Derek Veit 1, 1 8 9. That's not always the case when the list is generated dynamically. Joe Chung 9, 1 14 Something along the lines of Sign up or log in StackExchange. Sign up using Facebook.
HTML DOM Select add() Method
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.