I'm trying to add an EventListener to all Input Elements constantly.
But when I add the EventListener the function is triggered.
window.onload = window.setInterval(function () {
var frame = document.getElementsByTagName("FRAME");
for (var i = 0; i < frame.length; i++) {
frame_doc = frame[i].contentWindow.document || frame[i].contentDocument;
var elem = frame_doc.getElementsByTagName("INPUT");
for (var j = 0; j < elem.length; j++)
if (elem[i]) {
alert(true);
//if(typeof elem[i].onclick === "function")
//removeEvent(elem[i], "click", check());
addEvent(elem[i], "click", check());
}
}
}, 50);
function addEvent(elem, event, fn) {
if (elem.addEventListener) {
elem.addEventListener(event, fn, false);
} else {
elem.attachEvent("on" + event, function () {
return (fn.call(elem, window.event));
});
}
}
function check() {
//Do Something
}
Because you're calling the function when binding the event on new element.
When you use check()
when binding the event, the function check
is called. You can remove the ()
of the check
addEvent(elem[i], "click", check());
Should be
addEvent(elem[i], "click", check); // removed () of check