javascript - jQuery toggle function fails after a while -


i've written jquery toggling function looks this:

// category filter $.fn.clicktoggle = function(a, b) {   var ab = [b, a];   function cb(){ ab[this._tog^=1].call(this); }   return this.on('click', cb); };  $.fn.swapclass = function(oldclass, newclass) {   return $(this).removeclass(oldclass)                 .addclass(newclass)                 .fadein('fast'); };  $('.category-filter').clicktoggle(function() {   // clear old   $('.category-filter').each(function() {     $(this).fadeto('fast', 1.0);     $(this).swapclass('label-success', 'label-default');   })    // new   var category = $(this).attr('data-category');   $(this).swapclass('label-default', 'label-success');    // apply new   $('.category-filter').each(function() {     if (!($(this).attr('data-category').indexof(category) >= 0)) {       $(this).fadeto('fast', 0.5);     }   })   $('.article_container').each(function() {     if ($(this).attr('data-categories').indexof(category) >= 0) {       $(this).fadein('fast');     } else {       $(this).fadeout('fast');     }   }) }, function() {   $(this).swapclass('label-success', 'label-default');   $('.article_container').each(function() { $(this).fadein('fast') })   $('.category-filter').each(function() { $(this).fadeto('fast', 1.0) }) }); 

it works okay—see jsfiddle here—but after clicking 1 link , "skipping" click link toggle seems break.

what's wrong function? how can change toggles whether user clicks same link again or link next?

it because though you've changed style of other elements when click on one, haven't changed ._tog value.

you trying maintain state in 2 different ways. 1 through ._tog , through classes label-success/label-default. suggest not using ._tog @ , rely on classes. in fact, i'd simplify whole javascript.

update sorry, quick answer, have run, here ya go: https://jsfiddle.net/uh123qwx/3/

$('.category-filter').click(function(){      $('.category-filter').not(this).removeclass('active');      $(this).toggleclass('active');  });
.category-filter { background-color: #777; transition: .5s ease-in-out; }  .category-filter.active { color: green; background-color: #5cb85c; }    .category-filter.active[data-category='custom article'] ~ div > .article_container:not([data-categories*='custom article']),  .category-filter.active[data-category='retirement'] ~ div > .article_container:not([data-categories*='retirement']),  .category-filter.active[data-category='property'] ~ div > .article_container:not([data-categories*='property'])  {      display:none;  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <span class="label category-filter" data-category="custom article">custom article</span>  <span class="label category-filter" data-category="retirement">retirement</span>  <span class="label category-filter" data-category="property">property</span>    <div>    <div class="article_container" data-categories="custom article">      <p>custom</p>    </div>    <div class="article_container" data-categories="custom article">      <p>another custom</p>    </div>    <div class="article_container" data-categories="retirement">      <p>retirement</p>    </div>    <div class="article_container" data-categories="property">      <p>property</p>    </div>    <div class="article_container" data-categories="property retirement">      <p>property , retirement</p>    </div>  </div>


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -