jquery - Javascript not working on content appended via ajax on scroll -


i have been working on adding more content page scroll reaches end of page.

the problem javascript not working on content loaded via ajax

below simple code:

html:

    <div class="ajax_load_wrapper">         <div id="title_191">         <div id="title_172">         <div id="title_171">         <div id="title_105">         <div id="title_95">          <!-- new divs load here on jquery events don't work -->     </div>      <div id="load_more_vbz" style="display: none;">         <a href="somelink">loading...</a>     </div> 

jquery:

(function ($) {      $(window).on('scroll',function(){         if (($(window).scrolltop() == $(document).height() - $(window).height() )){             load_mode_content();         }     });      //loads more content     function load_mode_content(){         var link = $('#load_more_vbz a').attr('href');          $('#load_more_vbz').show(); //show loading image          $.get(link).done( function(data){             $('.ajax_load_wrapper').append(data); //appends new data             $('#load_more_vbz').hide();         });         return false;     }  })(jquery); 

if you're loading content dynamically , want jquery event handlers work in content, have 1 of these things:

  1. use delegated event handling work dynamically created content , event handlers can put in place before content there.

  2. install event handlers after dynamically created content in dom.

if using statically added event handlers, won't work on new dom elements because dom elements didn't exist @ time jquery selector query run , event handlers added elements existed @ time.

if show content , event handlers you're trying have work in dynamically loaded content, more specifically.

see these previous discussions how use .on() dynamic content:

jquery .live() vs .on() method adding click event after loading dynamic html

does jquery.on() work elements added after event handler created?

jquery .on not work .live does

proper use of .on method in jquery

this general idea, explained more in above references:

$("static selector").on('click', "dynamic selector", fn); 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -