0

This is the function I want to call (it definitely works I tested it):

function enableCategoryPopup(event) {
    event.stopPropagation();
    var buttonOffset = $(this).offset();
    $('.category-popup').css({
      top: buttonOffset.top + 10,
      left: buttonOffset.left +10
    });
    $('.category-popup').show(100);
  }

And this is where I want to call it:

$(document).on("mousedown", ".tab-links li:nth-child(2)", function(event) {
    if (event.which == 3) {
      enableCategoryPopup();
    }
  });

My custom function should be called as soon as I right click the specified element but it doesn't. When I call alert("x") instead of enableCategoryPopup() it works, only my custom function doesn't get executed.

What am I doing wrong? How do I call my custom function?

5
  • 4
    Pass event object to function, enableCategoryPopup(event); Commented Nov 30, 2015 at 13:34
  • enableCategoryPopup(event); please :) Commented Nov 30, 2015 at 13:35
  • I'm afraid that doesn't seem to work :( Commented Nov 30, 2015 at 13:38
  • Post HTML and code in a snippet Commented Nov 30, 2015 at 13:39
  • FYI, in your code, the propagation of event stop at document level, not at event target (".tab-links li:nth-child(2)"). So describe how does your code isn't working??? BTW, if you stop mousedown event to any ancestor of ".tab-links li:nth-child(2)", this delegated event won't be fired Commented Nov 30, 2015 at 13:47

1 Answer 1

3

try handling the event from within the enableCategoryPopup function

function enableCategoryPopup(event) {
    if (event.which == 3) {
        event.stopPropagation();
        var buttonOffset = $(this).offset();
        $('.category-popup').css({
          top: buttonOffset.top + 10,
          left: buttonOffset.left +10
        });
        $('.category-popup').show(100);
    }
  }

  $(document).on("mousedown", ".tab-links li:nth-child(2)", enableCategoryPopup);
Sign up to request clarification or add additional context in comments.

5 Comments

awesome, happy to help
Ya but doesn't explain why OP's code wasn't working?!
my guess would be timing, or the function wasn't declared before the binding
Using referenced method as in your answer (which is the relevant way of doing it), this method has to be defined before (could be hoisted) unlike in OP's code. I'm not sure what you mean by timing but anyway, if that fixes OP's issue, get my vote... :)
thanks @A.Wolff, what I meant was that the click event was still happening before enabledCategoryPopup was able to stopPropagation(). OP could have also tried event.prevendDefault()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.