webwrks
webwrks

Reputation: 11918

Blocking a click event from another click event

I have the following code:

$(function() {
    $(li).click(doThis);

    $(a.clickMe).live('click', doThat);
});

And I want to block doThis when a.clickMe is clicked. a.clickMe is located inside of the li... I want to check and see where the origin of the click came from, so I can see if a.clickMe was clicked.

Thanks!

Upvotes: 0

Views: 64

Answers (2)

nnnnnn
nnnnnn

Reputation: 150010

"I want to check and see where the origin of the click came from"

OK, so perhaps something like this:

$(function() {
    $("li").click(function(e) {
       if ($(e.target).is("a.clickMe"))
          return doThat.call(this, e);
       else
          return doThis.call(this, e);
    });
});

That is, within a click handler for the li element(s) test whether the event.target element is one of the "a.clickMe" elements and if so call doThat() (setting this and passing the event object). Otherwise call doThis().

Upvotes: 1

Blender
Blender

Reputation: 298106

You can prevent the click event from propagating up to the li:

$(a.clickMe).live('click', function(e) {
    e.stopPropagation();

    // Now, the event won't bubble up to the `li` and won't trigger its event handler.
});

Upvotes: 0

Related Questions