Reputation: 1121
when i try to pass arguments in addEventListener it behaves unusual
var animateFunction = function (i) {
if (animBool) {
Fn.slideData(values(i)); // To fetch some data
util.animate.tweenAnimate(sliderWrapper, 0 , Math.ceil("-"+sliderWidth));
animBool = 0;
} else {
util.animate.tweenAnimate(sliderWrapper, Math.ceil("-"+sliderWidth) ,0);
animBool = 1;
}
};
for (i = 0; i < annotateArray.length; i++) {
//To Remember the state of value "i"
(function (i) {
//Custom Event Listener for fallback for IE
util.addEvent(annotateArray[i], "click", animateFunction(i), true);
}(i));
}
for (i = 0; i < annotateArray.length; i++) {
//Custom Remove Event Listener for fallback for IE
util.removeEvent(annotateArray[i], "click", animateFunction);
}
//Custom Bind Event Method
util.addEvent = function (obj, evt, callback, capture) {
if (window.attachEvent) {
obj.attachEvent("on" + evt, callback);
} else {
if (!capture) {
capture = false;
}
obj.addEventListener(evt, callback, capture);
}
};
I am trying to bind event dynamically to all the elements but wen i click on the element the function doesn't behave as expected
Upvotes: 0
Views: 1638
Reputation: 71918
You're actually passing undefined
as an event handler, not an actual callback. Here:
util.addEvent(annotateArray[i], "click", animateFunction(i), true);
You're invoking the function, which returns undefined
. You must pass a function reference to addEventListener
. You already have something "to Remember the state of value 'i'" in your loop, but you're not using it correctly. It should be:
for (i = 0; i < annotateArray.length; i++) {
//To Remember the state of value "i"
(function (i) {
// Custom Event Listener for fallback for IE
util.addEvent(annotateArray[i], "click", function() {animateFunction(i)}, true);
}(i));
}
Upvotes: 1