Thursday, April 25, 2024
 Popular · Latest · Hot · Upcoming
rated 0 times [  123] [ 3]  / answers: 1 / hits: 80702  / 12 Years ago, thu, september 27, 2012, 12:00:00

I need to print the selected option ID with Javascript not JQuery for both select tags.

Assume we have more than one select tags.

<select  onchange=showOptions(this) id=my_select1>
<option value=a1 id=ida1>Option1</option>
<option value=a2 id=ida2>Option2</option>

<select onchange=showOptions(this) id=my_select2>
<option value=b1 id=idb1>Option1</option>
<option value=b2 id=idb2>Option2</option>

I found out the following way options[selectedIndex].id but how can I know to which one of those that line refers to..

Any suggestions?

I Tried the following but it did not work.

<select id=my_select onchange=showOptions2(this)>
<option value=o1 id=id1>Option1</option>
<option value=o2 id=id2>Option2</option>

<script type = text/javascript>

function showOptions2(s){
var adVALUE = console.log(s[s.selectedIndex].value); // get value
var adID = console.log(s[s.selectedIndex].id); // get id



More From » html

<select onchange=showOptions(this)>

this function will do the work

function showOptions(s) {
console.log(s[s.selectedIndex].value); // get value
console.log(s[s.selectedIndex].id); // get id

Note that, unless you are using them for other purpose, you may omit the id on select elements

Example jsbin :

[#82874] Wednesday, September 26, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.

Total Points: 424
Total Questions: 104
Total Answers: 101

Location: Serbia
Member since Tue, Jul 26, 2022
2 Years ago