Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
67
rated 0 times [  70] [ 3]  / answers: 1 / hits: 17924  / 9 Years ago, mon, may 11, 2015, 12:00:00

I have been searching for a way to get the id from a drop-down list option, when it is selected. I have found the following:
Get selected text from a drop-down list (select box) using jQuery

And tried to change the accepted answer to:



$(#yourdropdownid option:selected).id;


But when I alert() it, it gives me undefined. Is there a way to get the id using JQuery?


More From » jquery

 Answers
34

Because $(#yourdropdownid option:selected) returns a jQuery object which does not have the id property so, you can use .attr() to get id of the element



$(#yourdropdownid option:selected).attr('id');

[#66650] Friday, May 8, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
sageallenv

Total Points: 458
Total Questions: 102
Total Answers: 104

Location: Venezuela
Member since Thu, Jul 15, 2021
3 Years ago
;