This is a very common mistake that new web developers make, and I’ve made it myself. Using bind to element is a bad idea because you only need to bind the same code to element every time because jQuery is smart enough to look at the code in the current location of the element and update the DOM accordingly.
The key to binding jQuery is to put it in the current, current location of the element. The example code below is just that.
This is the same method I’ve explained in my previous tutorial that binds to multiple elements using.add(),.addClass(), and.removeClass().
The code below binds to elements and uses the code in the.on() method to run it as a method, thus binding to more than one element on the page. The example code is the same except instead of.on() it uses the code in the.on() method.
When it comes to jQuery there are really two ways you can use it. You can use it with a function or a statement. When you use it with a function, you are actually calling a function in the.on method that you created. When you use it with a statement, it basically runs the code that you put in the.on method.
What’s nice about jQuery is that it’s a very versatile tool.