5

I'm forced to use a script loaded from an external server.

This script basically adds an element <div class="myClass"> and bind a click method to it.

The thing is, in the click function associated to the element, they have a return false statement at the end.

I also have my own script and I'm trying to add a click method to the same element using $(document).on('click', '.myClass', function() { ... })

My problem is that their event is triggered before and the return false in their function doesn't trigger my own click method.

I've tried loading my script before theirs but that didn't fix the problem. I've read about unbinding and then rebinding but I'm not sure it's a good option since their code can change at any moment.

Anything else I could try?

3
  • 1
    well one way is pass event as argument and use event.preventDefault() for your script. Commented Sep 6, 2013 at 3:57
  • @DipeshParmar I'm not sure I understand. Where would I pass this event? Commented Sep 6, 2013 at 4:00
  • could you try event namespace $(document).on('click.namespace', '.myClass', function() { ... })? Commented Sep 6, 2013 at 4:06

5 Answers 5

5

You need to make your handler function return false.. it prevents the event from bubbling.

In your tag html you have to write something like:

<button type="button" class="btn" onclick="myHandler(); return false;"></button>

Or if you use jQuery:

$(".btn").on('click', function (event){ 
    //do stuff..
    return false;
});
Sign up to request clarification or add additional context in comments.

1 Comment

Although this is true, this was not what I asked but thanks anyway.
2

The problem is that event delegation depends on the event bubbling up to the element that you bind the handler to. When their handler returns false, that prevents bubbling.

You'll have to bind the handler directly to the elements after they're added:

$(".myClass").click(function() { ... });

1 Comment

This works! Thank you. I was trying to write less code with the .on method but it seems I can't. Thank you!
1

I had the same issue just recently. How I fixed it is, I added another class onto that element:

$(document).load(function() {
    $(".myClass").addClass("myNewClass");
});

and than binded click events to that class like so:

$(document).on("click", ".myNewClass", function () { ... }); 

This worked for me, as it overwrote the myClass class with the myNewClass click event.

Comments

1

Try this one:

$(document).on('click', '.myClass', function(e) {
   e.preventDefault();
   ... 
   ...
})

Comments

0

in your onLoad why don't you add a new class to the myClass div and then set up a event listener for the new class.

$(".myClass").addClass("myClass2");

$(".myClass2").on('click', function() { ... })

Comments

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.