jQuery Filter divs By Class

terf

I'm trying to filter divs by their class, but I'm having some trouble. For some reason, if any button is pressed, everything disappears. I've been troubleshooting this for a while now and I can't figure out why this is happening. A CodePen is here and my attempt is below:

HTML:

<button class="active btn" id="all">Show All</button>
<button class="btn" id="a">Show A</button>
<button class="btn" id="b">Show B</button>
<button class="btn" id="c">Show C</button>
<button class="btn" id="d">Show D</button>
<br />
<div class="a b">a &amp; b</div>
<div class="a">a</div>
<div class="b">b</div>
<div class="c">c</div>
<div class="d">d</div>

CSS:

div {
  background: #eee;
  height: 100px;
  width: calc(25% - 10px);
  float: left;
  margin: 5px;
  text-align: center;
}
.active {
  color: red;
}

jQuery:

$( document ).ready(function() {

  $("#all").click(function() {
    $("div").fadeIn(500);
    $("#all").addClass("active");
    $(":not(#all)").removeClass("active");
  });
  $("#a").click(function() {
    $(".a").fadeIn(500);
    $(":not(.a)").fadeOut(0);
    $("#a").addClass("active");
    $(":not(#a)").removeClass("active");
  });
  $("#b").click(function() {
    $(".b").fadeIn(500);
    $(":not(.b)").fadeOut(0);
    $("#b").addClass("active");
    $(":not(#b)").removeClass("active");
  });
  $("#c").click(function() {
    $(".c").fadeIn(500);
    $(":not(.c)").fadeOut(0);
    $("#c").addClass("active");
    $(":not(#c)").removeClass("active");
  });
  $("#d").click(function() {
    $(".d").fadeIn(500);
    $(":not(.d)").fadeOut(0);
    $("#d").addClass("active");
    $(":not(#d)").removeClass("active");
  });

});
Arun P Johny

It is because of the selector $(":not(.b)") which selects all elements and hides it except .b which will include the body element also.

You need to use a more specific selector, one option is to use a container element like

var $btns = $('.btn').click(function() {
  if (this.id == 'all') {
    $('#ct > div').show();
  } else {
    var $el = $('.' + this.id).show();
    $('#ct > div').not($el).hide();
  }
  $btns.removeClass('active');
  $(this).addClass('active');
})
.active {
  color: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button class="active btn" id="all">Show All</button>
<button class="btn" id="a">Show A</button>
<button class="btn" id="b">Show B</button>
<button class="btn" id="c">Show C</button>
<button class="btn" id="d">Show D</button>
<br />
<div id="ct">
  <div class="a b">a &amp; b</div>
  <div class="a">a</div>
  <div class="b">b</div>
  <div class="c">c</div>
  <div class="d">d</div>
</div>


Using a common selector like an element selector div also may not work as it could target other non related elements, so another option is to use a common class to all elements that need to be targeted.

var $items = $('.item');
var $btns = $('.btn').click(function() {
  if (this.id == 'all') {
    $items.show();
  } else {
    var $el = $('.' + this.id).show();
    $items.not($el).hide();
  }
  $btns.removeClass('active');
  $(this).addClass('active');
})
.active {
  color: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button class="active btn" id="all">Show All</button>
<button class="btn" id="a">Show A</button>
<button class="btn" id="b">Show B</button>
<button class="btn" id="c">Show C</button>
<button class="btn" id="d">Show D</button>
<br />
<div class="item a b">a &amp; b</div>
<div class="item a">a</div>
<div class="item b">b</div>
<div class="item c">c</div>
<div class="item d">d</div>

Note: Also as you can see, there is no need to write separate click handlers for each button - given that the id of the button and the class you are looking for is the same.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

jQuery Filter divs By Class display restriction

From Dev

jQuery class increment on divs

From Dev

Filter divs based on class with checkboxes

From Dev

Jquery - text filter to hide divs

From Dev

Jquery - text filter to hide divs

From Dev

jQuery filter the displaying of divs issue

From Dev

jquery css to a class in multiple divs

From Dev

jquery css to a class in multiple divs

From Dev

jQuery UI Slider and Checkbox to filter set of divs

From Dev

How to filter subset of isotope divs using jquery?

From Dev

How to filter subset of isotope divs using jquery?

From Dev

Filter by checkbox show/hide target divs jquery

From Dev

jQuery cookie to remember class of multiple divs

From Dev

Jquery click on generated divs with the same class

From Dev

Selecting divs with jQuery with the same class name

From Dev

jQuery count divs and add class after 20

From Dev

Issue with jquery animate with multiple divs (same class)

From Dev

Jquery: controlling duplicate class's (or divs) seperatly

From Dev

jquery how to count divs in row with different class

From Dev

Using jQuery on the select class not all the divs with the same class

From Dev

hide or filter divs depending on value of textbox using jquery

From Dev

Jquery Mobile Filter system, can't hide multiple divs

From Dev

hide or filter divs depending on value of textbox using jquery

From Dev

jQuery - Change CSS of one div that shares its class with other divs

From Dev

How to add hover class to jQuery click(function() accordian divs?

From Dev

Jquery: Adding css class to certain divs based on attributes

From Dev

jquery - add a class to random sets of divs with infinite loop

From Dev

How to remove a class with jQuery from all divs not just siblings?

From Dev

JQuery - Loop through DIVs with the same class and create Array for each of them

Related Related

  1. 1

    jQuery Filter divs By Class display restriction

  2. 2

    jQuery class increment on divs

  3. 3

    Filter divs based on class with checkboxes

  4. 4

    Jquery - text filter to hide divs

  5. 5

    Jquery - text filter to hide divs

  6. 6

    jQuery filter the displaying of divs issue

  7. 7

    jquery css to a class in multiple divs

  8. 8

    jquery css to a class in multiple divs

  9. 9

    jQuery UI Slider and Checkbox to filter set of divs

  10. 10

    How to filter subset of isotope divs using jquery?

  11. 11

    How to filter subset of isotope divs using jquery?

  12. 12

    Filter by checkbox show/hide target divs jquery

  13. 13

    jQuery cookie to remember class of multiple divs

  14. 14

    Jquery click on generated divs with the same class

  15. 15

    Selecting divs with jQuery with the same class name

  16. 16

    jQuery count divs and add class after 20

  17. 17

    Issue with jquery animate with multiple divs (same class)

  18. 18

    Jquery: controlling duplicate class's (or divs) seperatly

  19. 19

    jquery how to count divs in row with different class

  20. 20

    Using jQuery on the select class not all the divs with the same class

  21. 21

    hide or filter divs depending on value of textbox using jquery

  22. 22

    Jquery Mobile Filter system, can't hide multiple divs

  23. 23

    hide or filter divs depending on value of textbox using jquery

  24. 24

    jQuery - Change CSS of one div that shares its class with other divs

  25. 25

    How to add hover class to jQuery click(function() accordian divs?

  26. 26

    Jquery: Adding css class to certain divs based on attributes

  27. 27

    jquery - add a class to random sets of divs with infinite loop

  28. 28

    How to remove a class with jQuery from all divs not just siblings?

  29. 29

    JQuery - Loop through DIVs with the same class and create Array for each of them

HotTag

Archive