Having this simple dropdown menu:
<select id="foo">
<option>bar</option>
</select>
And an jQuery listener initialization like this:
$("#foo").on("click", function() {
console.log("stuff");
});
The event is only fired when the user closes the drop down, either by selecting an option or by clicking outside of the box. Is there any way to get the event, when he opens the box?
Here we will discuss how to handle the dropdown selected index change event using JQuery. In one of the requirements, we had one dropdown list which has options like Happy, Satisfied, and Sad. We need to show a comment box when the user selects Sad from the dropdown list. Else the comment box should be hidden. First, we are creating an HTML file.
Event methods trigger or attach a function to an event handler for the selected elements. The following table lists all the jQuery methods used to handle events. Deprecated in version 3.0. Use the on () method instead.
How to Open Dropdown on Hover Using jQuery attr () The jQuery hover () works when the users hover over the HTML elements. You have to perform displaying the select options when a hover event happens or trigger. Also, use the jQuery attr () to get the size of select and increase it on hover.
The short answer is: use the hover event with the jQuery attr () function to show select dropdown. If you are using the select box on your form, you may see its dropdown will open only on click.
The right event for this purpose is change click
together and will get fire every time that select input changed or clicked.
$("#foo").on("click change", function(e) {
$("#output").html("Event type: " + e.target.nodeName);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="foo">
<option value="1">foooo</option>
<option value="2">bar</option>
</select>
<div id="output"></div>
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