Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

removeEventListener won't remove

I know this question is asked a lot, but I must be overlooking something, as I can’t find a figure out why I can’t remove the event listner in the code below.

Can someone help me out? What am I missing?

function winResize() {

    viewportWidth = window.innerWidth;
    viewportHeight = window.innerHeight;

    const $sidebar = document.querySelector('.sidebar');
    const $purchaseFormFixed = document.querySelector('form.purchase-form');

    function checkFromTop() {
        ...
    }

    if ( viewportWidth >= '1000' ) {
        let sidebarHeight = $sidebar.offsetHeight;
        let space = viewportHeight - (sidebarHeight + 50);

        if ( space > '0' ) {
            window.removeEventListener('scroll', checkFromTop, false);
            $sidebar.classList.add('sticky');
            $purchaseFormFixed.classList.remove('show');
        } else {
            window.addEventListener('scroll', checkFromTop, false);
            $sidebar.classList.remove('sticky');
        }
    }

}
window.addEventListener('resize', winResize);
winResize();    

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Move the checkFromTop function definition outside of winResize so that the same function reference is passed to removeEventListener.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading