-1

I need to create a button whose function is to scroll to the top of the page and then print it.

My code attached to the button is the following:

<script>
jQuery(document).ready(
    function($){
        $('#backToTop').click(function(){
            $("html, body").animate({ scrollTop: 0 }, 500, function() {             
                window.print();
            });
        });
    }
);
</script>

And the button

<a id="backToTop">Print</a>

but the print method runs twice. I can't find a solution to prevent printing from running twice.

How can this be fixed?

meyi
  • 7,574
  • 1
  • 15
  • 28
Fabien
  • 3
  • 1
  • That's because you have 2 selectors at the same time `html, body`. `html` tag will hit that animation first, then `body` tag – Tân Feb 12 '20 at 14:28

1 Answers1

1

The event will fire twice because of your selector html, body.

What is event bubbling and capturing? this might come in handy.

bendb
  • 28
  • 5