Jquery .on('scroll') not firing the event while scrolling
You probably forgot to give #
before id for id selector, you need to give #
before id
ie is ulId
You problably need to bind scroll event on div that contains the ul and scrolls. You need to bind the event with div instead of ul
$(document).on( 'scroll', '#idOfDivThatContainsULandScroll', function(){
console.log('Event Fired');
});
Edit
The above would not work because the scroll event does not bubble up in DOM which is used for event delegation, see this question why doesn't delegate work for scroll?
But with modern browsers > IE 8 you can do it by other way. Instead of delegating by using jquery you can do it using event capturing with java script document.addEventListener
, with the third argument as true
; see how bubbling and capturing work in this tuturial.
Live Demo
document.addEventListener('scroll', function (event) {
if (event.target.id === 'idOfUl') { // or any other filtering condition
console.log('scrolling', event.target);
}
}, true /*Capture event*/);
If you do not need event delegation then you can bind scroll event directly to the ul instead of delegating it through document
.
Live Demo
$("#idOfUl").on( 'scroll', function(){
console.log('Event Fired');
});
Binding the scroll event after the ul has loaded using ajax has solved the issue. In my findings $(document).on( 'scroll', '#id', function () {...}) is not working and binding the scroll event after the ajax load found working.
$("#ulId").bind('scroll', function() {
console.log('Event worked');
});
You may unbind the event after removing or replacing the ul.
Hope it may help someone.
Using VueJS I tried every method in this question but none worked. So in case somebody is struggling whit the same:
mounted() {
$(document).ready(function() { //<<====== wont work without this
$(window).scroll(function() {
console.log('logging');
});
});
},