Monday, May 20, 2024
103
rated 0 times [  106] [ 3]  / answers: 1 / hits: 90627  / 11 Years ago, thu, march 7, 2013, 12:00:00

All I'm trying to do is to call a function when a DIV is scrolled.For simplicity sake Im not specifying anything else. Also I am only looking at DOM compliant browsers like Chrome, Safari (not IE).



MY problem is that the scroll handler never gets called. If I replace the scroll to click , it works when I click. Somehow the scroll is not working.



Please note: I cannot use jQuery :(



Here is my code:



HTML:



<div id=test>--long content--</div>


JS:



   function myFunc() {
console.log('in myFunc');
}
var objTable = document.getElementById(test);

objTable.addEventListener(scroll, function () {
myFunc();
}, false);


FIDDLE:



http://jsfiddle.net/yymg5/7/


More From » event-handling

 Answers
5

This is because the window is scrolling not the div. Try changing your element listener to the parent of the div (in this case the window) like this.



window.addEventListener(scroll, function () {
myFunc();
}, false);

[#79755] Wednesday, March 6, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
brendan

Total Points: 426
Total Questions: 110
Total Answers: 94

Location: Western Sahara
Member since Mon, May 3, 2021
3 Years ago
;