0

I have a fairly simple script that listens for a click event on a class this works fine on the first time it is clicked. but the problem is when an additional button is pressed it will execute the function more than once. I have read this is due to each new button press creates a new event handler but how am I suppose to resolve this?

the JQuery code I have is

$('.ButShare').on('click', function() {
    var id = $(this).attr('id');
    alert(id);
});

This is the simplified code that I have which still displays the symptoms

Regards

Vip32

7
  • 3
    That code does not produce the error that you describe. See here: jsfiddle.net/vngf7p1t You're most likely hooking up the click handler somewhere that is causing it to get bound repeatedly. Commented Sep 16, 2015 at 16:36
  • 2
    @PranavCBalan That's not gonna help him figure out what is causing the unexpected behaviour.. Commented Sep 16, 2015 at 16:40
  • 1
    possible duplicate of JQuery click class event triggers multiple clicks Commented Sep 16, 2015 at 16:40
  • "this works fine on the first time it is clicked. but the problem is when an additional button is pressed it will execute the function more than once." Is expected result to attach click event to only first .ButShare element ? Commented Sep 16, 2015 at 16:43
  • may be it's parent also have same class Commented Sep 16, 2015 at 16:44

0

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.