I want to respond to the user selecting an item in a select element. Yet this jQuery:
$('#platypusDropDown').select(function () {
alert('You selected something');
});
...does nothing. It shows no alert, although jsFiddle sees it as valid jQuery.
The Click event works, but it's too fast - it fires on clicking the select element, prior to making a selection.
Of course, I really want to do something like:
$('#platypusDropDown').select(function () {
var selection = $('platypusDropDown').val;
$.getJSON('platypus.json', selection, data() {
// . . .
});
});
The HTML:
<select id="platypusDropDown">
<option value="duckbill">duckbill</option>
<option value="duckbillPlatypus">duckbillPlatypus</option>
<option value="Platypus">Platypus</option>
<option value="Platypi">Platypi</option>
</select>
You need to use change event
$('#platypusDropDown').change(function () {
var selection = this.value; //grab the value selected
$.getJSON('platypus.json', selection, data() {
. . .
});
});
Fiddle
Plus $('platypusDropDown').val
here val should be val()
and you dont need to create a jquery object again over the element, this
inside the handler represents DOM element (select on which the event is bound to) and you can directly access the value with this.value
or $(this).val()
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