0

I want to do some filter by jQuery This is my HTML

<ul id="filters" class="filter nav nav-pills">
      <li><a class="filter_link" href="#" data-category="7">Shop</a></li>
      <li><a class="filter_link" href="#" data-category="8">Portal</a></li>
      <li><a class="filter_link" href="#" data-category="9">Website</a></li>
</ul>

<div class="col-lg-3 filter_div" data-postcat="9">
   some html
</div>

<div class="col-lg-3 filter_div" data-postcat="8">
   some html
</div>

and this is my JS

jQuery(document).on('click', '.filter_link', function() {
    var catId = jQuery(this).data("category");
    jQuery('.filter_div').each(function(i, el) {
        var termId = jQuery(el).data('postcat');

    });
});    

i can`t understand how i can hide and show elements by click if i click on ul > li > a with attribute for example 8. i want hide all divs with data-category not 8

3
  • Use .attr("data-category")instead of .data("category") Commented Aug 26, 2014 at 9:47
  • with .data in alert(catId) i see my current number. I `am interesting how hide DIVs with this number in data-postcat=catId Commented Aug 26, 2014 at 9:51
  • is this what you want? Commented Aug 26, 2014 at 9:52

3 Answers 3

1

You can use the attribute selector

jQuery(document).on('click', '.filter_link', function() {
    var catId = jQuery(this).data("category");
    //use attribute selector to find the items to be shown
    var $targets = jQuery('.filter_div[data-postcat="'+catId+'"]').show();
    //hide all the other items
    jQuery('.filter_div').not($targets).hide();
});  

Demo: Fiddle

Sign up to request clarification or add additional context in comments.

Comments

0

You can do it like this:

$(document).on('click', '.filter_link', function() {
    var catId = $(this).data("category");
     //hide all div
    $('.filter_div').hide();
    //show only specific div
    $(".filter_div[data-postcat='" + catId +"']").show();

});

fiddle

Comments

0

You have to filter the list using the jQuery attribute selector:

JavaScript

function showCategory(categoryId) {
    var $allDivs = jQuery('.filter_div[data-postcat]');
    var $selectedDiv = $allDivs.filter('[data-postcat='+categoryId+']');

    $allDivs.hide();
    $selectedDiv.show();
}

jQuery(document).on('click', '.filter_link', function(event) {
    var categoryId = event.target.getAttribute('data-category');

    showCategory(categoryId);

    event.preventDefault();
});

HTML

<ul>
      <li><a class="filter_link" href="#" data-category="7">Shop</a></li>
      <li><a class="filter_link" href="#" data-category="8">Portal</a></li>
      <li><a class="filter_link" href="#" data-category="9">Website</a></li>
</ul>

<div class="filter_div" data-postcat="9">
   category 9
</div>

<div class="filter_div" data-postcat="8">
   category 8
</div>

Here is a working JSFiddle for this: http://jsfiddle.net/pomeh/e04kxbhc/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.