Gustavo Henrique Gustavo Henrique - 5 months ago 16
Javascript Question

Scrolling Event not working JQuery

I have this div:

<div id="inbox-prof-load-msg" class="message-content row nice-scroll">
<?php include_once('inbox-chat-msg.php'); ?>
</div>


And i'm trying to get the
Scroll()
of this div:

$('div#inbox-prof-load-msg').scroll(function() {
console.log('Scrolled')
});


Why i'm not getting the Scroll event? I've made a
.click()
call to test, and the Javascript doesn't got the click event on the div too.
Someone know how to fix it?

NOTE : The element
div#inbox-prof-load-msg
is added dynamically to the DOM.

Thanks.

Answer

Use event delegation on() when you deal with fresh DOM (elements added dynamically to the DOM) :

$('body').on('scroll', 'div#inbox-prof-load-msg', function() {
    console.log('Scrolled')
});

$('body').on('click', 'div#inbox-prof-load-msg', function() {
    console.log('Clicked')
});

Hope this helps.

Comments