Jquery Scroll event is not firing
Matthew Barrera
I am trying to trigger an scroll event using class element. I tried multiple scenarios but no good.
Below are types of events tried...
jQuery(function($){ $('[class="ag-body-container"]').bind('scroll',function() { alert("hai"); }); });
$(document).ready(function(){ $(".ag-body-viewport-wrapper").bind('scroll',function() { alert("hai"); });
});
$(document).on('scroll', '.ag-body-container', function(){ console.log('scroll happened');
});
$(function() { $(".ag-body-container").on('scroll', function () { console.log('scroll happened'); });
});
$(".ag-body-container").bind('scroll', function() { console.log('Event worked');
}); Non of the above gives expected result.
3 Answers
you havent tried
$(document).ready(function(){ $(document).on('scroll', '.ag-body-container', function(){ console.log('scroll happened'); });
});OR
$(document).ready(function(){ $(window).on('scroll', '.ag-body-container', function(){ console.log('scroll happened'); });
}); 1 try
.oninstead of.bindor you can use.scroll.
Because, .bind may be removed from future versions at any time. There is no reason to keep using .bind and every reason to prefer .on instead.
The scroll event occurs when the user scrolls in the specified element.
The scroll event works for all scrollable elements and the window object (browser window).
if window is scrollable than the below code will fired.
$(function() { $(window).on('scroll', function () { console.log('scroll happened'); });
});but if you are trying to something like this.
$(".ag-body-container").on('scroll', function () { console.log('scroll happened'); });if the element identified by '.ag-body-container' is scrollable , then only event will be fired.
check the plunker :