By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,383 Members | 2,070 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,383 IT Pros & Developers. It's quick & easy.

how to get <option> id

P: 38
hey every body

how can i get using javascript the selected option <option> ID or Value because when ever i clicked it i get the <select> id and i dont want that

[HTML]<select name="list" id="list" size="10%" >
<option id="3" ondblclick="select_song();">Apple
<br> </option>
<option id="4" ondblclick="select_song();">Orange
<br> </option>
<option id="5" ondblclick="select_song();">Banana<br> </option>
<option id="6" ondblclick="select_song();">lemon<br> </option>
</select>[/HTML]

Expand|Select|Wrap|Line Numbers
  1. function select_song()
  2. {
  3.     var i = event.srcElement.id
  4.                alert(i); // this give me an alert (list)
  5.  
  6. }
Jun 25 '08 #1
Share this Question
Share on Google+
9 Replies


10K+
P: 13,264
Why not just go through a Javascript tutorial to find out how to use a select?
Jun 25 '08 #2

P: 38
i can teach you how to use javascript and if you dont know the answer plz dont replay

and dont be disrespectial cuz u r an admin

thanks anyway
Jun 25 '08 #3

10K+
P: 13,264
i can teach you how to use javascript and if you dont know the answer plz dont replay

and dont be disrespectial cuz u r an admin

thanks anyway
It's not that I want to be disrespectful.
We just have some rules on this forum which unfortunately I have to enforce.
Giving you the code makes your current problem go away but reading a tutorial makes future potential problems go away as well. There are also lots of threads in this forum which can give you the answer, but I still prefer that you read a Javascript tutorial on how to use a select.

Reading, unfortunately has become a lost art.
Jun 25 '08 #4

hsriat
Expert 100+
P: 1,654
I guess it too hot these days.. month of July approaching.. I hate it. I have ordered some soft drinks for you guys.

While we are waiting for the drinks, let me look into the JavaScript problem.
Expand|Select|Wrap|Line Numbers
  1. var list = event.srcElement;
  2. alert(list.options[list.selectedIndex].id);
I didn't test it. But I think it should work.

You may also read this tutorial.
Jun 25 '08 #5

10K+
P: 13,264
I guess it too hot these days.. month of July approaching.. I hate it. I have ordered some soft drinks for you guys.

While we are waiting for the drinks, let me look into the JavaScript problem.
Expand|Select|Wrap|Line Numbers
  1. var list = event.srcElement;
  2. alert(list.options[list.selectedIndex].id);
I didn't test it. But I think it should work.

You may also read this tutorial.
July is winter for us.

P.S You just gave someone fish instead of teaching them how to fish. Better stick around in case they run out of the fish that you gave them.
Jun 25 '08 #6

hsriat
Expert 100+
P: 1,654
July is winter for us.

P.S You just gave someone fish instead of teaching them how to fish. Better stick around in case they run out of the fish that you gave them.
umm.. then you must be in Aus or NZ I guess. Or somewhere in Southern Hemisphere.

And I linked a small tutorial too to let him know how I fish.

Regards,
Harpreet [ doen't not really intend to hurt anyone :) ]
Jun 25 '08 #7

acoder
Expert Mod 15k+
P: 16,027
srcElement is IE-only. target is the standard, so you'll have to use a combination to support all browsers.

One more problem: IDs shouldn't begin with numbers.

Note also that IE treats the options as part of the select (can be annoying), but not all browsers do.
Jun 25 '08 #8

P: 38
I guess it too hot these days.. month of July approaching.. I hate it. I have ordered some soft drinks for you guys.

While we are waiting for the drinks, let me look into the JavaScript problem.
Expand|Select|Wrap|Line Numbers
  1. var list = event.srcElement;
  2. alert(list.options[list.selectedIndex].id);
I didn't test it. But I think it should work.

You may also read this tutorial.
thanks hsriat

it works and thanks for the tutorial ,,,,
Jun 26 '08 #9

hsriat
Expert 100+
P: 1,654
thanks hsriat

it works and thanks for the tutorial ,,,,
As acoder said, srcElement is an IE thing. So you better call that function with an argument which refers to the select box.
selectSong(this.parentNode)
Jun 26 '08 #10

Post your reply

Sign in to post your reply or Sign up for a free account.