Jquery Scroll Event Is Not Firing

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 .on instead of .bind or 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.

See jquery .bind() vs. .on()

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 :

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

David Miller

David Miller

Executive Financial & Market Analyst

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.

Share this article
Twitter Facebook Pinterest