Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery Event Listener on DropDown Open

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?

like image 958
Watte Avatar asked Jan 15 '16 11:01

Watte


People also ask

How to handle dropdown selected index change event using jQuery?

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.

What is the use of event event in jQuery?

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?

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.

How to show select dropdown in form using jQuery?

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.


1 Answers

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>
like image 182
Vahid Msm Avatar answered Sep 19 '22 12:09

Vahid Msm