Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
<header class="page-header">
  <h1>jQuery Filter Buttons</h1>
  <div class="intro">
    <h2>What's happening</h2>
    <div class="intro-text">
      <p>
        This example shows how to use <a href="https://jquery.com">jQuery</a> to filter items. In this case we are using the <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/data-*">data attribute</a> to identify the type of card and the buttons. Then jQuery is used to respond to the user clicking the buttons and to filter the cards.
      </p>
      <p>All of the buttons have a .button class and are inside of an element with class .filters. All of the cards have a .card class and are in an element with class .cards.</p>
    </div>
    </div>
<div class="example">
    <p>Here is an example of a button with the data attribute:</p>
       <div class="code"> <code>&lt;button class="button" <span class="highlight" >data-type="sky"</span>&gt;sky&lt;/button&gt</code></div>
      <p>And this button would filter out all elements except for those with a matching <span class="highlight"><code>data-type="sky"</code></span> like the div below:</p>
<div class="code"><code>&lt;div class="card" <span class="highlight">data-type="sky"</span>&gt;</code></div>
  </div>
  <div class="additional">
    <p>The other buttons, CITY and LANDSCAPE, have matching <span class="highlight">"city"</span> and <span class="highlight">"landscape"</span> values for data-type. They filter cards with those data-type values.
      <br>
      The "all" button shows all cards.</p>
    
    <p class="note">
      Because this only uses a few of the basic jQuery functions, you can also use the lighterweight <a href="https://zeptojs.com/">Zepto</a> library instead without changing the code.
    </p>
  </div>
</header>


<section class="filterable">
  <div class="filters">
    <button class="button active" data-type="all">all</button>
    <button class="button" data-type="sky">sky</button>
    <button class="button" data-type="city">city</button>
    <button class="button" data-type="landscape">landscape</button>
  </div>
  
  <div class="cards">
    
<!--     div.card>figure.card_image>img[src="https://picsum.photos/seed/ddd/400/300"]^div.card_text>p -->
    
    <div class="card" data-type="city" data-location="NYC">
      <figure class="card_image">
        <img src="https://picsum.photos/seed/1232132/400/300" alt="">
      </figure>
      <div class="card_text">
        <p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Deserunt numquam voluptate molestias provident sunt! Facilis.</p>
      </div>
    </div>
    <div class="card" data-type="landscape">
      <figure class="card_image">
        <img src="https://picsum.photos/seed/abcd/400/300" alt="">
      </figure>
      <div class="card_text">
        <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Iure, veniam? Commodi perferendis maxime magnam tempora?</p>
      </div>
    </div>
    <div class="card" data-type="sky">
      <figure class="card_image"><img src="https://picsum.photos/seed/xyz/400/300" alt=""></figure>
      <div class="card_text">
        <p>
          Lorem ipsum, dolor sit amet consectetur adipisicing elit. Adipisci fugiat nobis libero, quis officiis possimus!
        </p>
      </div>
    </div>
    <div class="card" data-type="sky">
      <figure class="card_image"><img src="https://picsum.photos/seed/ccc/400/300" alt=""></figure>
      <div class="card_text">
        <p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Odio, amet aspernatur natus incidunt veniam eveniet.
        </p>
      </div>
    </div>
    <div class="card" data-type="city">
      <figure class="card_image"><img src="https://picsum.photos/seed/678/400/300" alt=""></figure>
      <div class="card_text">
        <p>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Accusamus nisi magni, maxime eveniet id itaque.</p>
      </div>
    </div>
    <div class="card" data-type="landscape">
      <figure class="card_image"><img src="https://picsum.photos/seed/111ddd/400/300" alt=""></figure>
      <div class="card_text">
        <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Minus quae laboriosam tempore aspernatur vero temporibus? </p>
      </div>
    </div>
    <div class="card" data-type="city">
      <figure class="card_image"><img src="https://picsum.photos/seed/2ddd/400/300" alt=""></figure>
      <div class="card_text">
        <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Architecto soluta quas possimus praesentium, excepturi aspernatur?</p>
      </div>
    </div>
    <div class="card" data-type="sky">
      <figure class="card_image"><img src="https://picsum.photos/seed/3ddd/400/300" alt=""></figure>
      <div class="card_text">
        <p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Inventore, eius quia tenetur pariatur maiores doloribus?</p>
      </div>
    </div>
    <div class="card" data-type="landscape">
      <figure class="card_image"><img src="https://picsum.photos/seed/dddf/400/300" alt=""></figure>
      <div class="card_text">
        <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Fugit itaque rerum possimus dolore qui sint?</p>
      </div>
    </div>
    
  </div>
  
</section>

<footer>
  <p>Some HTML and CSS adapted from <a href="https://html5up.net/phantom">Phantom template</a> on <a href="https://html5up.net/">HTML5 UP</a>.</p>
  <p>Images from <a href="https://picsum.photos/">Lorem Picsum</a></p>
</footer>
              
            
!

CSS

              
                /* Basic Responsive Images */
img{
  width: auto; 
  height: auto;
  max-width: 100%; 
}

.highlight{
  color: lightblue;
}





/* 
==============================
CARD LAYOUT and STYLING
============================== */
.cards{
  display: grid;
  grid-gap: 1rem;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  padding: .5rem;
}

.card{
  box-shadow: 0px 1px 5px #555;
  box-shadow: 10px 15px 10px rgba(0, 0, 0, 0.08);
  border-radius: 5px;
  overflow: hidden;
  visibility: visible;
  opacity: 1;
  transition: visibility 500ms, opacity 500ms;
}
.card.hidden{
  visibility: hidden;
  display: none;
  opacity: 0;
  transition: visibility 0s linear 500ms, opacity 500ms;
}
.card_text{
  padding:.5rem;
}

figure{
  padding:0;
  margin: 0;
}


/* 
==============================
BUTTONS
============================== */
.filters{
  padding:.5rem;
}

/* from Phantom Template, see footer for link */
.button{
  -moz-appearance: none;
		-webkit-appearance: none;
		-ms-appearance: none;
		appearance: none;
		-moz-transition: background-color 0.2s ease-in-out, color 0.2s ease-in-out, box-shadow 0.2s ease-in-out;
		-webkit-transition: background-color 0.2s ease-in-out, color 0.2s ease-in-out, box-shadow 0.2s ease-in-out;
		-ms-transition: background-color 0.2s ease-in-out, color 0.2s ease-in-out, box-shadow 0.2s ease-in-out;
		transition: background-color 0.2s ease-in-out, color 0.2s ease-in-out, box-shadow 0.2s ease-in-out;
		background-color: transparent;
		border-radius: 4px;
		border: 0;
		box-shadow: inset 0 0 0 2px #585858;
		color: #585858 !important;
		cursor: pointer;
		display: inline-block;
		font-size: 0.8em;
		font-weight: 900;
		height: 3.5em;
		letter-spacing: 0.35em;
		line-height: 3.45em;
		overflow: hidden;
		padding: 0 1.25em 0 1.6em;
		text-align: center;
		text-decoration: none;
		text-overflow: ellipsis;
		text-transform: uppercase;
		white-space: nowrap;
    margin-bottom: .5rem;
}

.button.active{
  background-color: #585858;
			color: #ffffff !important;
}



/* 
==============================
HEADER AND FOOTER STYLING
============================== */
a, a:active, a:visited{
  color: yellow;
}
.page-header, footer{
  margin-top: 3rem;
  padding: 1rem;
  background-color: #555;
  color: #fff;
  line-height: 1.5;
}



/* 
==============================
HEADING
============================== */
.page-heading{
  background-color: #555;
}

@media screen and (min-width:800px){
  .intro, .additional{
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    grid-gap: 1rem;
  }
  
  .intro h2{text-align:right;}
  
  .additional{
    border-top: 1px solid #fff;
  }
  
  .example{
    max-width:63ch;
    margin:1rem auto;
  }
  
  .example .code{
/*     margin: 0; */
    padding: 1rem;
    border:1px dashed #eee;
  }
  
}
              
            
!

JS

              
                //only run the jQuery code when the document is ready
$(document).ready(function() {
  //code to respond to clicking one of the buttons in div.filters
  $('.filters .button').click(function() {
    
    // remove active class from other buttons
    $('.filters .button').removeClass('active');
    
    // add active class to this button
    $(this).addClass('active');
    
    // fetch the value of the data-type attribute of the button clicked
    let btnType = $(this).attr('data-type');
    console.log(btnType); //this is just for testing to see if we have the attribute value we expect

    //if they click a button with data-type="all" then show all cards
    if(btnType === 'all') {
      // show all our items
      $('.cards .card').show();
    }
    else {
      let attribute = '[data-type="'+btnType + '"]'; //this is not necessary, but saves us having to write it out twice below.
      
      //hide everything that does not have the attribute of the button
      $('.cards .card').not(attribute).hide();

      //show everything that does have the attribute
     $('.cards .card').filter(attribute).show();
 
    }
    
    return false;
  });
});
              
            
!
999px

Console