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 :
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;
}
});
}
});