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?
$('#mySelectBox option'). each(function() { if ($(this). isChecked()) alert('this option is selected'); else alert('this is not'); });
To detect real-time changes, you want the input event. document. addEventListener('input', function (event) { // Only run on our select menu if (event.target.id !== 'wizard') return; // Do stuff... }, false);
Syntax of jQuery Select Option$(“selector option: selected”); The jQuery select option is used to display selected content in the option tag. text syntax is below: var variableValue = $(“selector option: selected”).
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; } });
What you need to do is add an onchange
handler to the select
:
$('#type').change(function(){ if($(this).val() == 2){ /* Do Something */ } });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With