orpqK
orpqK

Reputation: 2785

Select the value from a drop down menu

How do I make my drop down menu select the value and display it when I select the item let go of my mouse button? BUT NOT when I click the little arrow drop down button.

HTML:

<select name="menu" id="menu"></select>

JS:

$("#menu").click(function () {
    var currentSelection = select.options[select.selectedIndex].value;
    populate(select.options[select.selectedIndex].value);

});

Problem is is that when I click the down arrow from the selection menu, it thinks I have clicked/selected an item already, and it calls the populate() function right there. How can I change it to only call populate() after I select an item and left click it?

Upvotes: 0

Views: 70

Answers (3)

.change()

$("#menu").change(function () {
    //code here
});


Better use this Keyword

$("#menu").change(function () {
    var currentSelection = this.value;
    populate(this.value);

});

Upvotes: 2

Krish R
Krish R

Reputation: 22711

Try this, You need to use change handler instead of click handler

$("#menu").change(function () {
    populate($(this).val());
});

Upvotes: 0

Jason P
Jason P

Reputation: 27012

Try .change() instead:

$("#menu").change(function () {

So the event only fires when the value changes.

You can simplify your inner code too:

populate(this.value);

Upvotes: 3

Related Questions