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 7066939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:08:28+00:00 2026-05-28T05:08:28+00:00

I created jQuery sorter plugin like below to filter by classes. But now I

  • 0

I created jQuery sorter plugin like below to filter by classes. But now I am not sure how to only display class="apple" by default. Right now when I reload the page both APPLE and TREE is visible.

<ul>
  <li class="apple">APPLE</li>
  <li class="tree">TREE</li>
  <li class="apple tree">ALL</li>
</ul>

<div>
  <div class="apple">APPLE</div>
  <div class="tree">TREE</div>
  <div class="apple">APPLE</div>
  <div class="tree">TREE</div>
  <div class="apple">APPLE</div>
  <div class="tree">TREE</div>
</div>

<script src="/js/jquery.min.js"></script>
<script type="text/javascript">
$("ul li").click(function() {
  visibleClasses = $(this).attr("class").split(" ");
  $("div div").hide(); // or slideUp / fadeOut
  for(i in visibleClasses) {
    $("div div."+visibleClasses[i]).fadeIn(500); // or slideDown / show
  }
});
</script>
  • 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-05-28T05:08:29+00:00Added an answer on May 28, 2026 at 5:08 am

    Hide all non-apples with:

    $("div div:not(.apple)").hide();
    

    Rather than iterate the array, construct a selector that get’s all the elements at once:

    var selector = "div div." + visibleClasses.join(",div div.");
    

    selector will contain "div div.apple,div div.tree" which will select elements that match either of the comma separated selectors. You can then do:

    $(selector).fadeIn(500);
    

    Edit: So, with this change, your code would look like this:

    <script type="text/javascript">  
        $(function () {
            $("div div:not(.apple)").hide();
            $("ul li").click(function() {
                var visibleClasses = $(this).attr("class").split(" ");
                $("div div").hide();
                var selector = "div div." + visibleClasses.join(",div div.");
                $(selector).fadeIn(500);
            });
        });
    </script>
    

    By, the way, if you choose to iterate the Array use a regular for loop or Array.forEach(). Do not use for..in to iterate an Array. The for..in statement is for enumerating, not iterating.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I created jQuery plugin to sort class with class=apple and class=tree but how do
I created a jQuery plugin but I have a problem, I use the following
I've created a jQuery accordion for my website, it all works very fine. But
I created a jquery toggle, but when I click the link to open a
i have created a jquery onhover vertical sliding menubar now i need to implement
I have created a jQuery elastic plugin. I have the problem that if I
I've created a jQuery UI button, and would like to add a 'X' secondary
I have created a jQuery content switcher . Generally, it works fine, but there
this might be an easy question but for me right now it is not
Scenario: Using jquery form plugin . The form appears in a dialog, created with

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.