Prevent binding of events multiple times in jQuery

Either newbie or experienced developer, everyone occasionally stuck with a situation that it’s  event binding runs in a loop or multiple times and it runs unexpected and we are unable to find the bug because we bind the event only one time but in practical it binds so multiple times.

So for proper functioning of the events, we need to prevent binding of events multiple times in jQuery.  It is very simple, just we need to unbind that event before its binding so that if it is binded before then it will be unbinded .

Example:

This code snippet will create the  unexpected result but by adding single line of code, we can resolve this problem.

We unbind the event before its binding like :

 

You Might Interested In

Leave a Reply

Enclose a code block like: <pre><code>Your Code Snippet</code></pre>.