Filter Div Section based on Select box (Drop Down List) using Javascript | Sanwebcorner

Filter Div Section based on Select box (Drop Down List) using Javascript

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');
  }
});




Post a Comment

Labels

360 degree view product image 3d rotate 3d text 3step-form Accordion menu ajax All Browsers Animated Background Gradient animation audio controller auto complete Best accordion menu bootstrap border bounce effect on hover Breadcrumb calculate age by dob calendar Captcha change image on mouse move color chooser countdown Counter on scroll create random number Css css animation css buttons css hover css3 customized upload Button Date picker delete data from table Different scroll speed email otp verification email verifier Embed eventlist Expanding Menu feedbck form flash news section close button full screen menu Google custom search Google Maps Google reCAPTCHA Google site search gradient hide menu Horizontal News ticker Hover Htm Html html dropdown html form html5 html5 form validation Iframe image hover increment decrement textbox value based on select box Javascript Jquery js Layered image effect on mouse hover lock & unlock page scroll marquee marquee with close button mobile number validation mobile otp login Multi-level-drop-down-menu multi-level-form multi-select-checkbox Multi-Step-form Multiple File Upload in PHP mysql News ticker on hover change text to image our team page password generator password strength checker Photoshop PHP php mailer populate html selectbox Popup image on click the link pure css Question type form random number generate Random password Redirect page Based on the select box while submit the form responsive responsive menu responsive popup box Responsive Vertical Time line design rotate image selectbox based on select box simple coming soon page simple countdown based on end date simple typing effect slider sliding tab sms otp login statistic counter on scroll switch tag shape team member Time picker toggle toggle view transparent button underline animation up down slide validation vertical fixed sidebar Webtips whatsapp link website Youtube