David
David

Reputation: 15350

How to prevent jquery event firing on a drop down list given an event is attached

I have a drop down list on my webpage with a jquery event attached to the change event. On change I use ajax to load a partial view in a popup window.

When the user uses the keyboard to navigate through the list of items but then clicks a link on another part of the page I momentarily see my popup window. This is obviously happening as when the user clicks a link elsewhere on the page, the change event is triggered on the drop down first.

Are there any neat solutions to solve this problem? A simple solution would be to fire the event on keyup but I don't want this functionality to happen.

Here's the change event snippet of code

$('#lookup').change(function()
{
    $.get(..//load template

Upvotes: 3

Views: 659

Answers (4)

Jithendra R
Jithendra R

Reputation: 11

If its an ajax call, there is chance that your ajax call getting cached. Make sure you are disabling cache. something like "cache:false"

Upvotes: 1

David
David

Reputation: 15350

Did not find a solution that I liked or worked clean with my style so decided to catch trigger on both the 'change' and 'keyup' events so as to avoid this scenario.

Upvotes: 0

Hal Helms
Hal Helms

Reputation: 684

I'm a little confused by your question. But assuming that the "change" isn't a true change, I think you could keep a variable for "lastSelected" and, when a change occurs, check it against that. If it's the same, ignore it. If it's different, fire off your Ajax request and update the value of "lastSelected".

Upvotes: 0

Mauro
Mauro

Reputation: 2070

The event could not be stopped because the change event is fired in any case.

One possibility could be to set a certain timeout between the event fired and the actual call of the $.get method in order to take your case into account, but certainly it isn't an elegant solution.

Otherwise you may call the return function only if a certain other element gets the focus (for example if there is a textbox, after your dropdown list).

I would not care too much about it as it seems your popup HAS to be shown on Dropdown change. If it is not critical, you may have a help button on dropdown's right that popsup your window on button's click.

Upvotes: 0

Related Questions