-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
  • 21,590
  • 73
  • 223
  • 402
  • 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,164
  • 15
  • 164
  • 175
karthikr
  • 92,866
  • 25
  • 190
  • 186
1

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

DevlshOne
  • 8,177
  • 1
  • 27
  • 36