How to connect two filters in jquery content? Filter 1 and filter 2 connect result

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


How to connect two filters in jquery content? Filter 1 and filter 2 connect result



I'm newbie in jquery. I created filters in jquery and i want to connect them together to show content only clicked. How i can do that? Sorry for my bad english. I try many hours and I've building this:




$("button").click(function() {
var show = $(this).attr('class');
$('.post').each(function(){
$(this).show();
var test = $(this).attr('class');
if (test.indexOf(show) < 0) $(this).hide();
});
});


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h2>FIRST</h2>
<div id='filter'>
<button class='all'>All</button>
<button class='1'>One</button>
<button class='2'>Two</button>
</div>
<h2>TWO</h2>
<div id='filter'>
<button class='all'>All</button>
<button class='aa'>Washington</button>
<button class='bb'>Philadelphia</button>
</div>

<br>
<div id='posts'>
<div class='all post 1 aa'>One Washington</div>
<div class='all post 1 bb'>One Philadelphia</div>
<div class='all post 2 bb'>Two Philadelphia</div>
<div class='all post 2 aa'>Two Washington</div>
<div class='all post 1 bb'>One Philadelphia</div>
<div class='all post 2 aa'>Two Washington</div>
<div class='all post 1 bb'>One Philadelphia</div>
</div>





Share the source which you tried, and tell us what result you're expecting.
– Kamalakannan J
11 mins ago





You are considering each filter in a silo, never together. That is the crux of your issue.
– Taplar
9 mins ago





Also, you cannot repeat ids.
– Taplar
8 mins ago





I share, sorry my bad edit. I want connect filters. When i click "One" and click "Washinton" I want show only options "One Washington" and no other options. The same when i click Two and Washington or Philadelphia
– obiboke
7 mins ago





Sorry Taplar can i please to fiddle with correct issue for this? I dont understand many things
– obiboke
4 mins ago




1 Answer
1



See the comments in the logic for the details on what each step is doing.




$("button").click(function() {
//remove the selected class from the previously selected button
$(this).closest('.filter').find('button.selected').removeClass('selected');
//put the class on the button you clicked
$(this).addClass('selected');

//get the data-filter off of the selected buttons and join them into a selector
var filter = '.'+ $('button.selected').map(function(){
return $(this).data('filter');
}).get().join('.');

//hide all the posts, and then show those that match
$('.post').hide().filter(filter).show();
});


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h2>FIRST</h2>
<!-- we use a class because an id cannot be repeated
and we want to select both our filters later -->
<div class='filter'>
<!-- data fields let us separate the classes from the data that
is used for the filter -->
<button data-filter='all'>All</button>
<button data-filter='1'>One</button>
<button data-filter='2'>Two</button>
</div>
<h2>TWO</h2>
<div class='filter'>
<button data-filter='all'>All</button>
<button data-filter='aa'>Washington</button>
<button data-filter='bb'>Philadelphia</button>
</div>

<br>
<div id='posts'>
<div class='all post 1 aa'>One Washington</div>
<div class='all post 1 bb'>One Philadelphia</div>
<div class='all post 2 bb'>Two Philadelphia</div>
<div class='all post 2 aa'>Two Washington</div>
<div class='all post 1 bb'>One Philadelphia</div>
<div class='all post 2 aa'>Two Washington</div>
<div class='all post 1 bb'>One Philadelphia</div>
</div>






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to scale/resize CVPixelBufferRef in objective C, iOS

Stripe::AuthenticationError No API key provided. Set your API key using “Stripe.api_key = ”

SVG with two text elements. When one resizes due to textLength - how to resize the other one to the same character size