Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
183
rated 0 times [  186] [ 3]  / answers: 1 / hits: 37483  / 11 Years ago, thu, september 26, 2013, 12:00:00

I'm having some trouble with the scroll event.



I'm trying to attach/bind the event to a specific div,
and I'm using $('body').on() to do it, due to the fact that
the content is reloaded when sorting, so it will lose its binding.



This doesn't work, the event is not fired:



$('body').on('scroll', 'div.dxgvHSDC + div', function () {
}


This on the other hand works:



$('body').on('mousewheel DOMMouseScroll', 'div.dxgvHSDC + div', function () {
}


And this as well:



$('div.dxgvHSDC + div').on('scroll', function () {
}


What's the problem?


More From » jquery

 Answers
91

You can not add delegation to the scroll event. This event doesn't bubble up the DOM and therefore you can not delegate it to any element. You can find more information here:



The scroll event does not bubble up.


Although the event does not bubble, browsers fire a scroll event on both document and window when the user scrolls the entire page.



You will need to create the event handler inside the event which creates your scrolling element.


Living example: http://jsfiddle.net/Um5ZT/1/


$('#link').click(function(){

//dynamically created element
$('body').append('<div class="demo"></div>');

//dynamically added event
$('.demo').on('scroll', function () {
alert('scrolling');
});

});

[#75408] Wednesday, September 25, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tammyb

Total Points: 278
Total Questions: 101
Total Answers: 103

Location: Botswana
Member since Sat, Jan 7, 2023
1 Year ago
;