jQuery sending multiple ajax requests all by itself. Kind of.

TAGs: 6 Comment

Posted By: eligeske on in JQuery

jQuery sending out multiple of the same ajax calls when you really only wanted one.


$('.myButton').unbind().bind('click', myMethod);

Sometimes, less is more in this world and that’s how I like my ajax.

When creating an application or website that is using ajax most of the time you will be attaching an event to an element in the DOM. Or in plain english attaching an onClick event to a link or button.

If you are dynamically creating html, you may need to use the jquery .bind() method.

But if you don’t pay real close attention and recreate the same element using the same code, it will bind again.

So when you only press your button once, it is firing multiple clicks events because you used bind multiple times on the same element.

How do we stop this you ask?

Using the jquery .unbind() method before  .bind()  will unbind the already bound events to reset your element.


6 Responses to "jQuery sending multiple ajax requests all by itself. Kind of."

  1. Ken says:

    FINALLY found a solution to the issue I was having. I was using .change() to dynamically bind a function to a select box, and I guess binding it multiple times makes it fire multiple times. This solved my issue. Thanks!!

  2. Antoine says:

    Fixed an issue I was having with multiple comments posting. Thanks

  3. Laurence says:

    nice solution.

  4. Erol says:

    Finally!!! After days and days of multiple sessions, I found this.

  5. Jonas Goes says:

    Really nice! Not works using $(‘.myButton’).unbind().bind(‘click’, myMethod) but I solved my issue adding $(‘#foo’).unbind(‘click’); in .done(function(data) event attached with $.ajax. Thank You!

  6. Greg says:

    Thank you!

Learning DHTMLX Suite UI

Learn the foundation of the DHTMLX Suite quickly while building a single page application with multiple components in harmony.

Popular post