Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9002807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:37:12+00:00 2026-06-16T00:37:12+00:00

I know else if works on jQuery so where’s the problem in this code:

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:37:13+00:00Added an answer on June 16, 2026 at 12:37 am

    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 Boolean true). You should have written:

    if (document.location.href.indexOf('#1')>-1) {
    

    But since you appear to be comparing hashes, let’s just do those directly instead (and use the proper window.location while we’re at it):

    if (window.location.hash == '#1') {
        // ...
    } else if (window.location.hash == '#2') {
        // etc.
    

    That said, in your case, we can do this entirely without the if/else just by parsing that hash string:

    var hash = window.location.hash.substr(1); // remove leading #
    if (hash) {
        $(".products li").fadeOut();
        $(".products li."+hash).stop(true,true).fadeIn(200);
    } else {
        $(".products li").fadeIn();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok, I don't know what else to do. This code worked perfectly fine a
I don't know if anyone else even has this problem, or has noticed, but
I don't know if anyone ELSE has noticed this, but I noticed the jQuery
i have a problem with jQuery fadeIn(or fadeOut) method. i build an article rotator
This is my view code I used jQuery it works and I didn't have
this is my first question here. I have the following jquery code: $(document).ready(function(){ $(a).click(function(){
I'm using the popular jQuery validation plugin: Validation As many know, this plugin works
I do not know if this is specifically a jquery problem, actually I think
I know if/else works, but I needed an alternative. I am using B =
I don't think this is technically a macro but I don't know what else

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.