sapatos
sapatos

Reputation: 1304

jquery event propagation

I've got a web page and when I click on a link it opens a 'review this item' page. On this second page I can click the esc key to return to the previous page.

On the second page I have a button which creates a pop up form for choosing photos. On this pop up form I can click esc to return to the main second page.

Problem is that when I click esc in the pop up form it closes the pop up form and then returns to the first page. I.e. like I've clicked esc twice.

I'm aware of event propogation and have tried calling e.preventDefault(); prior to this sections of code but it doesn't appear to work.

Is there something i'm missing here?

Upvotes: 0

Views: 66

Answers (1)

bokonic
bokonic

Reputation: 1771

What you're looking for is the stopPropagation() method on the Event object. This can be called in a jquery event handler but it's just normal DOM/(pure js). So in jQuery:

$('#modal').on('keyup', function(e) {
  if (e.which === 27) {
    e.stopPropagation();
    // close the window
  }
});

That will keep the event from bubbling up the page to where your other handler is currently going back on the same event.

Upvotes: 3

Related Questions