Attach scroll event to div with body on() binding fails

Jompis

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?

Alvaro

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');
    });
    
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to attach scroll event on FancyBox

From Dev

Binding scroll event to iframe

From Dev

Binding scroll event to iframe

From Dev

Attach scroll position to opacity of a div

From Dev

How to attach a scroll event to a text input?

From Dev

How to attach a scroll event to a text input?

From Dev

Event listener fails to attach or remove in some contexts

From Dev

Scroll to next div on body click

From Dev

Attach an image to the top of the page, no scroll for img, in the body element

From Dev

binding scroll event doesn't work on AngularJS

From Dev

Scroll to bottom of div event (overflow)

From Dev

Scroll event not getting fired for a div

From Dev

Attach click event to body via button click without immediately triggering it

From Dev

Fixed fullscreen div over body with scroll bars

From Dev

Fixed fullscreen div over body with scroll bars

From Dev

Is there a performance penalty for binding multiple event handlers to the scroll event?

From Dev

scroll event not firing on div inside scroll-able div

From Dev

Scroll event is not firing on my div element

From Dev

scroll() event not firing when attached to a div

From Dev

detecting scroll event on a page-scoped div

From Dev

How to stop Scroll changed event from triggering on data binding (WPF)

From Dev

Scroll event not binding inside directive - angular.js

From Dev

how to attach a function to a div without event handler (which is dynamically loaded)

From Dev

How to disable scroll of body while scrolling on a fixed div without hiding scroll bar of body?

From Dev

Scroll div for certain amount of pixels: how to scroll div without causing scroll event

From Dev

Scroll fixed div horizontaly while body stays positioned

From Dev

Scroll fixed div horizontaly while body stays positioned

From Dev

Possible to scroll inside a div while body overflow hidden?

From Dev

How to make a scroll bar in separate div in body of html

Related Related

  1. 1

    How to attach scroll event on FancyBox

  2. 2

    Binding scroll event to iframe

  3. 3

    Binding scroll event to iframe

  4. 4

    Attach scroll position to opacity of a div

  5. 5

    How to attach a scroll event to a text input?

  6. 6

    How to attach a scroll event to a text input?

  7. 7

    Event listener fails to attach or remove in some contexts

  8. 8

    Scroll to next div on body click

  9. 9

    Attach an image to the top of the page, no scroll for img, in the body element

  10. 10

    binding scroll event doesn't work on AngularJS

  11. 11

    Scroll to bottom of div event (overflow)

  12. 12

    Scroll event not getting fired for a div

  13. 13

    Attach click event to body via button click without immediately triggering it

  14. 14

    Fixed fullscreen div over body with scroll bars

  15. 15

    Fixed fullscreen div over body with scroll bars

  16. 16

    Is there a performance penalty for binding multiple event handlers to the scroll event?

  17. 17

    scroll event not firing on div inside scroll-able div

  18. 18

    Scroll event is not firing on my div element

  19. 19

    scroll() event not firing when attached to a div

  20. 20

    detecting scroll event on a page-scoped div

  21. 21

    How to stop Scroll changed event from triggering on data binding (WPF)

  22. 22

    Scroll event not binding inside directive - angular.js

  23. 23

    how to attach a function to a div without event handler (which is dynamically loaded)

  24. 24

    How to disable scroll of body while scrolling on a fixed div without hiding scroll bar of body?

  25. 25

    Scroll div for certain amount of pixels: how to scroll div without causing scroll event

  26. 26

    Scroll fixed div horizontaly while body stays positioned

  27. 27

    Scroll fixed div horizontaly while body stays positioned

  28. 28

    Possible to scroll inside a div while body overflow hidden?

  29. 29

    How to make a scroll bar in separate div in body of html

HotTag

Archive