skip to Main Content

i wanted to get the scroll method and load somthing while scrolling happens .
its not working properly
maybe because my elements are being added to the page dynamically.
however i see the existance of the elements but its not working programmatically.
and while i add these methods into my chrome’s console then it start to work.
idk how to solve it.

        

    $(document).ready(function () {
                ////First Method
                $(".convHistory").on('scroll', function () {
            console.log('Event Fired');
        });
        
        ////Second Method
        $(".convHistory").bind('scroll', function () {
                console.log('Event worked');
                }); 

        
        ////Third Method
        document.addEventListener('scroll', function (event) {
            if (event.target.className.includes("convHistory")) {  
                console.log('scrolling', event.target);
            }
        }, true /*Capture event*/);
        
        


                //forth method
        $('.convHistory').scroll(function () {
            console.log('Event worked');

        });
        
        
        
        


    });

2

Answers


  1. As your elements are dynamic, bind events for dynamic elements like this:-

    $(document).on('scroll', '.convHistory', function () {
         console.log('Event worked');
    });
    
    Login or Signup to reply.
  2. As you already said, the elements are binding dynamically, you have to do something like below

    div.append(yourElement class="convHistory");
    

    then after that

    $(".convHistory").on('scroll', function () {
        console.log('Event Fired');
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search