April 28, 2015

Becareful with jquery on or bind event.

When you do jQuery and sometime you would bind some functions on an event outside appended DOM elements, and rerun the function again like this:

for example we want to change the text to something else when we click on each of the li, maybe lets change to "run test ...";

now you want append a new li and allow function on that appended li like this:


this situation it works well, however what actually happened is that you create the function two times on each event ('click'), if you click button again second time, you create three function for each li click, and so on...
so what you want to do is off the click first, then on again, like this.


let me know if you have anything to comment below.