-1

I have the following code:

<script type="text/javascript">
    $(function () {
        $(".wrapper1").scroll(function () {
            $(".wrapper2").scrollLeft($(".wrapper1").scrollLeft());
        });
        $(".wrapper2").scroll(function () {
            $(".wrapper1").scrollLeft($(".wrapper2").scrollLeft());
        });
    });
</script>

But my HTML is dynamic and added later. Is there a way that I can use this code outside of the dynamically added HTML so that it works after the HTML is loaded ?

Alan2
  • 23,493
  • 79
  • 256
  • 450
  • Did any of the people who marked this as duplicate check to see if it works with the scroll event :-( – Alan2 Nov 25 '13 at 20:41

2 Answers2

3

You can use event delegation. Use

$(document).on('scroll', '.wrapper1', function () {

instead of

$(".wrapper1").scroll(function () {

and similarly for all the dynamically loaded elements.

More info here

Kevin B
  • 94,570
  • 16
  • 163
  • 180
karthikr
  • 97,368
  • 26
  • 197
  • 188
1

Take a look at the .on() property to delegate dynamically created elements.

DevlshOne
  • 8,357
  • 1
  • 29
  • 37