In this post i am explain about how to Filter the div contents based on select box ( Drop down list). This is one of the useful concept for developers who need filters for div with the help of select button.
When you select the values from the drop down list, The corresponding Div will display with in the Webpage. This type of filters will use commonly in website in various places.
Styles:
.div
{
border:1px solid #ccc;
padding:25px;
border-left:4px solid red;
margin:20px;
}
select
{
width:500px;
height:45px;
border:1px solid red;
text-indent:25px;
}
Html Code:
<form style="margin-left:20px;">
<select class="event-type-select">
<option value="all">All Event Types</option>
<option value="music">Music</option>
<option value="food">Food</option>
<option value="art">Art</option>
<option value="fashion">Fashion</option>
<option value="sport">Sport</option>
<option value="history">History</option>
</select>
</form>
<div class="container">
<div class="div" data-eventtype="music">Music 1</div>
<div class="div" data-eventtype="food">Food 1</div>
<div class="div" data-eventtype="art">Art 1</div>
<div class="div" data-eventtype="fashion">Fashion 1</div>
<div class="div" data-eventtype="sport">Sport 1</div>
<div class="div" data-eventtype="history">History 1</div>
<div class="div" data-eventtype="music">Music 2</div>
<div class="div" data-eventtype="food">Food 2</div>
<div class="div" data-eventtype="art">Art 2</div>
<div class="div" data-eventtype="fashion">Fashion 2</div>
<div class="div" data-eventtype="sport">Sport 2</div>
<div class="div" data-eventtype="history">History 2</div>
<div class="div" data-eventtype="music">Music 3</div>
<div class="div" data-eventtype="food">Food 3</div>
<div class="div" data-eventtype="art">Art 3</div>
<div class="div" data-eventtype="fashion">Fashion 3</div>
<div class="div" data-eventtype="sport">Sport 3</div>
<div class="div" data-eventtype="history">History 3</div>
<div class="div" data-eventtype="music">Music 4</div>
<div class="div" data-eventtype="food">Food 4</div>
<div class="div" data-eventtype="art">Art 4</div>
<div class="div" data-eventtype="fashion">Fashion 4</div>
<div class="div" data-eventtype="sport">Sport 4</div>
<div class="div" data-eventtype="history">History 4</div>
<div class="div" data-eventtype="music">Music 5</div>
<div class="div" data-eventtype="food">Food 5</div>
<div class="div" data-eventtype="art">Art 5</div>
<div class="div" data-eventtype="fashion">Fashion 5</div>
<div class="div" data-eventtype="sport">Sport 5</div>
<div class="div" data-eventtype="history">History 5</div>
</div>
Javascript:
$( ".event-type-select" ).change(function() {
var selectedEventType = this.options[this.selectedIndex].value;
if (selectedEventType == "all") {
$('.container div').removeClass('hidden');
} else {
$('.container div').addClass('hidden');
$('.container div[data-eventtype="' + selectedEventType + '"]').removeClass('hidden');
}
});
1 Comments
Thanks for this!
ReplyDeleteAny way to make it work for multiple data attributes? Say you want to sort for music, but then sort again via decade. Or Art, and then decade? Or decade, and then sort by art or music?