Which option selected javascript




















In a single-selection list box the count of the selected items can be 0 if the list box has no selected element by default or 1. In a multiple-selection list box the count of the selected items may range from 0 to the number of the items. If only one selected element is allowed in the selection list, the selectedIndex property contains the index of the selected item in the options collection of the tag.

If you need the value of the selected option in a single-selection list, you can use the value property of the select element. If multiple selection is allowed, you can get the selected elements by iterating through the options collection of the tag and check the value of the selected property of every option tag. See Example 3 for details. Boolean that indicates the selected state of the object. One of the following values:.

Did you find this example helpful? Peter Mortensen 29k 21 21 gold badges 97 97 silver badges bronze badges. Carl Onager Carl Onager 3, 2 2 gold badges 33 33 silver badges 65 65 bronze badges.

There are two ways to get this done either using JavaScript or jQuery. So, I'm adding the getElementsByName solution just for new developers to see too. Nandula Perera 3 1 1 silver badge 4 4 bronze badges. Ben Greenaway Ben Greenaway 1 1 silver badge 2 2 bronze badges. Marvil Joy Marvil Joy 6 6 silver badges 14 14 bronze badges. This uses jQuery, which doesn't answer the OP's question.

Frank Conijn Frank Conijn 2, 1 1 gold badge 20 20 silver badges 28 28 bronze badges. Another solution is: document. Javad Kargar Javad Kargar 1 1 gold badge 8 8 silver badges 21 21 bronze badges. It is not cross-browser compatible. Jacob Archambault 2 2 silver badges 11 11 bronze badges. Olawale Oladiran Olawale Oladiran 8 8 silver badges 14 14 bronze badges.

Clairton Luz Clairton Luz 1, 18 18 silver badges 11 11 bronze badges. He wants not the value but the shown text of the select box — Thanasis. I think this is the actual answer most people are looking for. Good answer! It was useful — Dilip Agheda. Ani Menon Ani Menon Good answer for showing how the code needs to be refreshed after used! You can use querySelector. Rounin Rounin ThomAce ThomAce 3 3 silver badges 7 7 bronze badges. Genko Genko 4 4 silver badges 10 10 bronze badges.

Hector Llorens Hector Llorens 5 5 bronze badges. Heretic Monkey Heretic Monkey 11k 7 7 gold badges 49 49 silver badges bronze badges. Belgacem Ksiksi Belgacem Ksiksi 1 1 gold badge 5 5 silver badges 19 19 bronze badges. Pal Singh Pal Singh 1, 1 1 gold badge 14 14 silver badges 28 28 bronze badges.

Try ddlViewBy. I think you can attach an event listener to the select tag itself e. Mario Petrovic 4, 6 6 gold badges 29 29 silver badges 50 50 bronze badges. Emmanuel Onah Emmanuel Onah 2 2 silver badges 6 6 bronze badges.

Great answer, thank you! In my case, I have two selectors, but I was able to access the selector id with 'e. Here's an easy way to do it in an onchange function: event. Talking about simplicity, I was thinking about this instead of event. Just do: document. Knautiluz Knautiluz 3 3 silver badges 12 12 bronze badges.

Make a drop-down menu with several options As many as you want! Dangerousgame Dangerousgame 51 8 8 bronze badges. Select one from the given options:. The value of the option selected is:. Check option. How to get selected value in. Previous How to get selected text from a drop-down list using jQuery? Recommended Articles. Article Contributed By :.

Easy Normal Medium Hard Expert. Writing code in comment? Please use ide. Load Comments.



0コメント

  • 1000 / 1000