I know else if works on jQuery so where’s the problem in this code:
if (document.location.href.indexOf('#1')) {
$(".products li").fadeIn();
}
else if (document.location.href === '#2') {
$(".products li").fadeOut();
$(".products li.2").stop(true,true).fadeIn(200);
}
else if (document.location.href === '#3') {
$(".products li").fadeOut();
$(".products li.3").stop(true,true).fadeIn(200);
}
else if (document.location.href === '#4') {
$(".products li").fadeOut();
$(".products li.4").stop(true,true).fadeIn(200);
}
else if (document.location.href === '#5') {
$(".products li").fadeOut();
$(".products li.5").stop(true,true).fadeIn(200);
}
else if (document.location.href === '#6') {
$(".products li").fadeOut();
$(".products li.6").stop(true,true).fadeIn(200);
}
else {
$(".products li").fadeIn();
}
If i put only if instead of else if it works but it’s not correct.
The expression
document.location.href.indexOf('#1')will return -1 if no match is found, and zero if it matches at the start of the string. Since you test for falsey values, you’ll never have a false result (-1 evaluates as a Booleantrue). You should have written:But since you appear to be comparing hashes, let’s just do those directly instead (and use the proper
window.locationwhile we’re at it):That said, in your case, we can do this entirely without the
if/elsejust by parsing that hash string: