I am trying to write a simple jQuery plugin just to see how its done. But i cant seem to run it twice simultaneously. Its basically a count down and all it does is get the text() value in a div and count it down until it reaches 1.
$('#box1').startCounter();
$('#box2').startCounter();
This call changes both this variables inside the function to point to #box2. Here is my jsfiddle
Its pretty confusing how this changes around in a jQuery plugin. thanks for any help 🙂
You defined
$thisin global scope, so whenstartCountis called on the second element, the value is overwritten. Usevarto make it local:DEMO
Instead of invoking the function again the element, you could also do something like this:
DEMO 2
And to make your plugin work in environments where
$does not refer tojQuery(jQuery.noConflict()), you should do: