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

Why doesn't this jquery function work with multiple if statements?

Each one of the following if statements works perfectly as long as the other if statement isn’t present. I don’t understand why having both if statements causes the function not to work. I’ve tried so many variations but can’t figure it out. This is the code:

$("a").click(function(event){   
    event.preventDefault();
    let lItem = document.querySelectorAll(".listing-item");
    let mItem = document.querySelectorAll(".menu-item");    
    let destination = $(this).attr("href");

    if ( $(this).parent(lItem) )  {
        tlUp.tweenTo(0, {
            onComplete: () => {
                window.location = destination;
            }
        });
    }

    if ( $(this).parent(mItem) ) {
        tlMenu.tweenTo(0, {
            onComplete: () => {
                window.location = destination;
            }
        });
    }
});

>Solution :

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

Based on your given code, it seems like you want to check that if an immediate parent has a specific class then based on that perform some action.

In that case you need to modify your code like the below: (by using .hasClass())

$("a").click(function(event){   
    event.preventDefault();
    let destination = $(this).attr("href");

    if ( $(this).parent().hasClass('listing-item') )  {
        tlUp.tweenTo(0, {
            onComplete: () => {
                window.location = destination;
            }
        });
    }

    if ( $(this).parent().hasClass('menu-item') ) {
        tlMenu.tweenTo(0, {
            onComplete: () => {
                window.location = destination;
            }
        });
    }
});
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