Get selected option element javascript

Get selected option element javascript

Posted: Alekov Date of post: 02.06.2017

To get the selected option inside a select element, you can use selectedIndex , then you can use the options array to get to the selected option object.

html - Get selected option text with JavaScript - Stack Overflow

Ideally you should use value attribute for option tags and just use val method to get the selected value from the dropdown element. Change the id attribute to value and then use. By posting your answer, you agree to the privacy policy and terms of service. 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: Get current selected option Ask Question.

Mark Fondy 1, 6 15 Having that start with or are a number is invalid. I use also value, but for this example i showed only ID. You should have added that to the question. It would have saved an argument in the comments here.

This is kind of like the scenic route, except it doesn't look pretty. That's kind of harsh, I prefer Esailia's answer too since it's the same as mine but this could be considered easier to understand if you're a jQuery minded developer and the question does mention jQuery.

I am not a jQuery developer, I don't like procedural jQuery code so I wouldn't use it because it has an unneeded level of abstraction since straight JS code is even clearer.

Don't need to be overly critical. AndeE And do you have any non-trolling reasons why this is worse than Esailija's answer? To clarify why Esailija's one performs better: By using find, you're doing a query against albeit a small portion of the tree it of course depends on jQuery internals.

Esailija k 9 Rocket Hazmat k 23 The OP want's either 1, 2 or 3 depending on which option is selected. That's what this code does. The HTML was incorrect as well as the JavaScript.

get selected option element javascript

Sorry, I didn't notice that you swapped id with value Suggesting that they change the HTML doesn't teach the OP what the question was really asking I'm teaching them the correct solution. I don't want to show the OP the wrong way to do this. Better to do it the right way, then to try to make workarounds to solve problems created by doing things the wrong way.

javascript select element - Stack Overflow

Your solution assumes that what they want is the value property of the option. People usually simplify the problem to make it easier to answer. However, the OP also mentioned that he already uses value for something else, so this is not a good solution for what's he's doing. That's why I usually prefer to answer the question than to imply that the OP doesn't know what they're doing. Answers the question, but explains a better way to do it.

Badre 1 7. That won't give you the id, it will give you: Thanks for pointing out the obvious id vs value. I updated my answer. That's still not what was requested, you can't change the HTML. Why can't we change the HTML? After reviewing both your answers jrummel , I agree that this HTML is what the OP should be using.

HTML DOM Select value Property

It's a blurry line whether to answer the literal question, or to suggest a better approach Yogurt The Wise 2, 3 23 Sign up or log in StackExchange.

Sign up using Facebook.

Sign up using Email and Password. Post as a guest Name. Stack Overflow works best with JavaScript enabled.

How to Make JavaScript Dropdown List

Is there some reason you have ID attributes instead of value attributes on your options? No self-respecting developer should ever choose this method over Esailia's answer. OP is not asking for the value of the selected object.

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.

get selected option element javascript
Rating 4,4 stars - 843 reviews
inserted by FC2 system