Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
78
rated 0 times [  82] [ 4]  / answers: 1 / hits: 41336  / 15 Years ago, wed, december 23, 2009, 12:00:00

I'd like jQuery to detect when the option with the id trade_buy_max is selected.





$(document).ready(function() {
$(option#trade_buy_max).select(function () {
//do something
});
});

<script src=https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js></script>
<select name='type' id='type'>
<option id='trade_buy' value='1' selected='selected'>Buy</option>
<option id='trade_buy_max' value='1'>Buy max</option>
<option id='trade_sell' value='2'>Sell</option>
<option id='trade_sell_max' value='2'>Sell max</option>
</select>





I've tried the following, but it doesn't seem to work.



Any ideas?


More From » jquery

 Answers
40

This works...
Listen for the change event on the select box to fire and once it does then just pull the id attribute of the selected option.



$(#type).change(function(){
var id = $(this).find(option:selected).attr(id);

switch (id){
case trade_buy_max:
// do something here
break;
}
});

[#98007] Saturday, December 19, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
leslijessalyng

Total Points: 650
Total Questions: 85
Total Answers: 109

Location: Croatia
Member since Mon, Sep 6, 2021
3 Years ago
leslijessalyng questions
Fri, Feb 21, 20, 00:00, 4 Years ago
Tue, Jul 30, 19, 00:00, 5 Years ago
Fri, Jul 5, 19, 00:00, 5 Years ago
Wed, Mar 13, 19, 00:00, 5 Years ago
;