JavaScript Trick: Simplest and Shortest Code for Hiding and Displaying as to Make Accordions

There are many ways to hide and show content on a website, one of the simplest ways is to use inline event handlers, combined with ternary operators.

This method creates resuable accordion component using native code, which can be used in blogging content management system like this blog, Blogge,or WordPress. Just use the sample code below.

Example

topic 1
topic 2
topic 3

Sample Code

<style>
.accordion {
  background-color: #eee;
  color: #444;
  cursor: pointer;
  width: 100%;
  border: none;
  text-align: center;
  outline: none;
  font-size: 15px;
  transition: 0.4s;
}

.active, .accordion:hover {
  background-color: lightgrey; 
}
  
.content{
  background-color: white;
  text-align: left;
  padding:6px;  
}
</style>
<div class="accordion" onclick="this.childNodes[1].style.display = this.childNodes[1].style.display == 'none' ? 'block' : 'none'">
  topic 1 
  <div class="content" style="display: none;">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla eu tristique libero. Pellentesque aliquam libero eget tempor ultricies. Etiam fringilla tincidunt commodo. Aenean consectetur porta nisi ac rutrum. Nullam aliquam fringilla lacus, nec cursus sapien pulvinar nec. Aenean nec nisi quis lectus laoreet pretium at id odio. Praesent vehicula maximus justo in semper. Morbi mattis sollicitudin turpis, eget condimentum purus rutrum a. Pellentesque at ligula in ex volutpat scelerisque sed vitae sem. Aliquam tincidunt gravida tellus eu porta. Proin aliquet risus et lectus varius tempus.
  </div>
</div>

<div class="accordion" onclick="this.childNodes[1].style.display = this.childNodes[1].style.display == 'none' ? 'block' : 'none'">
   topic 2
  <div class="content" style="display: none;">
    Sed elementum semper mi, ac consequat mi iaculis eu. Morbi mattis velit sed egestas consequat. Sed ultrices, urna ac auctor consectetur, purus est volutpat urna, facilisis scelerisque tortor elit vel nulla. Curabitur dolor sapien, pellentesque sit amet vulputate vel, fringilla id est. Phasellus dictum diam sit amet pulvinar mattis. Sed aliquet ornare tortor, ac gravida urna porta in. Donec euismod sit amet ante in vestibulum. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae; Cras gravida metus id dolor vestibulum, vel euismod magna elementum. Nullam viverra tristique facilisis. Suspendisse malesuada imperdiet maximus.
  </div>
</div>

<div class="accordion" onclick="this.childNodes[1].style.display = this.childNodes[1].style.display == 'none' ? 'block' : 'none'">
   topic 3 
  <div class="content" style="display: none;">
    Donec a malesuada mauris, ac elementum erat. Nunc condimentum urna porta suscipit consectetur. Cras in ligula non lacus dictum pulvinar. Nulla fringilla metus eu lectus vestibulum, non aliquam libero pretium. Fusce vel dui molestie, vulputate purus ac, tincidunt nibh. Pellentesque in ullamcorper justo. Donec egestas pulvinar nisl ut lacinia. Ut eleifend sem nec elit dignissim ullamcorper. Etiam vehicula fermentum scelerisque.
  </div>
</div>

How to Use this Accordion Component with WordPress

  1. In the WP post editor, add a custom HTML block on the top of the post.
  2. Copy the <style>...</style> code and paste it in the block from the 1st step.
  3. Add the accordion html code <div class="accordion" ... </div> to where you want to show the accordion.
  4. Done

Basic Styling of this Accordion

  1. Content in the first div tag is the title like "topic 1". You can change it.
  2. Content in the  second div tag is the accordion content like "Lorem ipsum...". You can change it.
  3. Yon can change font size for all words in the accordion by changing the "font-size" style attribute in .accordion in the style tag.
  4. Play around with background-color, color, width, text-align and etc. to see the effects.

Comments

Popular posts from this blog

How to Make A Reusable Image Slideshow HTML Component With Vanilla JavaScript

HTML Tags and Inline CSS that Work In Plotly.js Title

How to Type Spaces In HTML Input And Display In the Browser