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

is it possible to simplify (JS)?

When I click on the list icon it changes to the arrow icon and vice versa, the code was extended because the change from one icon to another has an animation, thks

![](https://i.sstatic.net/BHYrRowz.png)

const f1 = document.querySelector('.f_img1');

const f2 = document.querySelector('.f_img2');
const f_button = document.querySelector('.footer button');

f_button.addEventListener('click', e => {

    if(f1.classList.contains('listAnimation') || f1.classList.contains('listAnimation_reverse')) {
        if(f1.classList.contains('listAnimation')) {
            f1.classList.replace('listAnimation', 'listAnimation_reverse');
            f2.classList.remove('arrowAnimation_reverse'); f2.classList.add('arrowAnimation');
        } else {
            if(f1.classList.contains('listAnimation_reverse')) {
                f1.classList.replace('listAnimation_reverse', 'listAnimation');
                f2.classList.remove('arrowAnimation'); f2.classLists.add('arrowAnimation_reverse');
            }
        }
    } else {
        if(f2.classList.contains('arrowAnimation') || f2.classList.contains('arrowAnimation_reverse')) {
            if(f2.classList.contains('arrowAnimation')) {
                f2.classList.replace('arrowAnimation', 'arrowAnimation_reverse');
                f1.classList.remove('listAnimation_reverse'); f1.classList.add('listAnimation');
            } else {
                f2.classList.replace('arrowAnimation_reverse', 'arrowAnimation');
                f1.classList.remove('listAnimation'); f2.classLists.add('listAnimation_reverse');
            }
        } else {
            f1.classList.add('listAnimation_reverse');
            f2.classList.add('arrowAnimation');
        }
    }

    
})

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 :

Yes, it is possible to simplify. Just use the toggle method.

f_button.addEventListener('click', e => {
    if (f1.classList.contains('listAnimation') || f1.classList.contains('listAnimation_reverse') {
        f1.classList.toggle('listAnimation');
        f1.classList.toggle('listAnimation_reverse');
    } else {
        f1.classList.add('listAnimation_reverse');
    }

    if (f2.classList.contains('arrowAnimation') || f2.classList.contains('arrowAnimation_reverse') {
        f2.classList.toggle('arrowAnimation');
        f2.classList.toggle('arrowAnimation_reverse');
    } else {
        f2.classList.add('arrowAnimation');
    }
});

If you add arrowAnimation to f2, and listAnimation_reverse to f1 in the HTML, you wouldn’t need the else parts, thus making it simpler.

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