javascript - Filter by checkbox show/hide div link jquery -


i have jquery checkbox filter works showing/hiding div, when added <a href="#"></a>the checkbox no longer shows/hides div

2 of div's links demonstrate, other 2 purposefully not links show work.

question needs added jquery enable checkbox act on div's when inside <a href>

i tried var selecteddivs = $('#categories > .linkkk > div').hide(); didn't seem work

https://jsfiddle.net/f7srx0dd/22/

<input type="checkbox" class="checkbox" name="high" data-category-type="high">high <input type="checkbox" class="checkbox" name="low" data-category-type="low" > low  <input type="checkbox" class="checkbox" name="low" data-category-name="bread" > bread    </div>  <div id="categories">     <a href="#" class="linkkk"><div class="hide" data-category-type="high" data-category-name="pizza">high</div></a>     <a href="#"><div class="hide" data-category-type="low" data-category-name="pasta">low</div></a>     <div class="hide" data-category-type="low" data-category-name="pizza">low</div>     <div data-category-type="high" data-category-name="bread">bread</div> </div>  $('.checkbox ').on('click', function (e) {    var $this = $(this),     $links = $('.checkbox');  if ($this.hasclass('selected')) {     $this.removeclass('selected'); } else {     $this.addclass('selected'); }   var selecteddivs = $('#categories > div').hide();  var anyselectedcheckbox = false;  $.each($links, function (k, v) {  $this = $(v);  if ($this.hasclass('selected')) {     anyselectedcheckbox = true;     var cat = $this.data('categorytype');     var nam = $this.data('categoryname');     selecteddivs = selecteddivs.filter('[data-category-type="'+cat+'"],   [data-category-name="'+nam+'"]'); }  }); selecteddivs.show();  if(!anyselectedcheckbox) { $('#categories > div').show();  }   }); 

(demo)

you're using css child selector not find div if not direct descendant of #categories. this...

var selecteddivs = $('#categories > div').hide(); 

should this...

var selecteddivs = $('#categories div').hide(); 

or, more specific, use this...

var selecteddivs = $('#categories > div, #categories > a').hide(); 

or, if want every direct descendant of #categories, can use this...

var selecteddivs = $('#categories > *').hide(); 

or, if want hide div within anchor, can use this...

var selecteddivs = $('#categories > > div').hide(); 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

php - Find a regex to take part of Email -

javascript - Function overwritting -