javascript - jQuery Filter divs By Class -



javascript - jQuery Filter divs By Class -

i'm trying filter divs class, i'm having trouble. reason, if button pressed, disappears. i've been troubleshooting while , can't figure out why happening. codepen here , effort 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"); }); });

it because of selector $(":not(.b)") selects elements , hides except .b include body element also.

you need utilize more specific selector, 1 alternative utilize container element like

class="snippet-code-js lang-js prettyprint-override">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'); }) class="snippet-code-css lang-css prettyprint-override">.active { color: green; } class="snippet-code-html lang-html prettyprint-override"><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 mutual selector element selector div may not work target other non related elements, alternative utilize mutual class elements need targeted.

class="snippet-code-js lang-js prettyprint-override">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'); }) class="snippet-code-css lang-css prettyprint-override">.active { color: green; } class="snippet-code-html lang-html prettyprint-override"><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 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: can see, there no need write separate click handlers each button - given id of button , class looking same.

javascript jquery html

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -