<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: AkshayKhot07</title>
    <description>The latest articles on Forem by AkshayKhot07 (@akshaykhot07).</description>
    <link>https://forem.com/akshaykhot07</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F487488%2F315d882d-39c1-461f-beba-2c93dbecd93a.png</url>
      <title>Forem: AkshayKhot07</title>
      <link>https://forem.com/akshaykhot07</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/akshaykhot07"/>
    <language>en</language>
    <item>
      <title>Creating an Accordion using vanilla Javascript</title>
      <dc:creator>AkshayKhot07</dc:creator>
      <pubDate>Wed, 24 Aug 2022 11:40:05 +0000</pubDate>
      <link>https://forem.com/akshaykhot07/creating-an-accordion-using-vanilla-javascript-3o55</link>
      <guid>https://forem.com/akshaykhot07/creating-an-accordion-using-vanilla-javascript-3o55</guid>
      <description>&lt;p&gt;Let's create a Facts Accordion using HTML, CSS and Vanilla Javascript&lt;/p&gt;

&lt;p&gt;Firstly we will create an HTML skeleton of the accordion. There will be 4 Facts card. So the HTML will look like the below :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    &amp;lt;div class="container"&amp;gt;
      &amp;lt;h2 class="accordion-title"&amp;gt;Facts About Mars!&amp;lt;/h2&amp;gt;
      &amp;lt;div class="faq-container grid"&amp;gt;

        &amp;lt;div class="faq-item"&amp;gt;
          &amp;lt;header class="faq-header"&amp;gt;
            &amp;lt;i class="ri-add-line"&amp;gt;&amp;lt;/i&amp;gt;
            &amp;lt;h4 class="faq-header-title"&amp;gt;Also known as the Red Planet&amp;lt;/h4&amp;gt;
          &amp;lt;/header&amp;gt;
          &amp;lt;div class="faq-desp"&amp;gt;
            &amp;lt;p class="faq-desp-content"&amp;gt;
              This is because Mars is covered in soil, rock, and dust made from
              iron oxide which gives the surface a red rusty colour
            &amp;lt;/p&amp;gt;
          &amp;lt;/div&amp;gt;
        &amp;lt;/div&amp;gt;

     &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There will be 3 more facts card [&lt;em&gt;div with a class of faq-item&lt;/em&gt;] inside the [&lt;em&gt;div with a class of faq-container&lt;/em&gt;]. We will use CSS grid to structure the facts cards:&lt;br&gt;
&lt;code&gt;.grid {&lt;br&gt;
  display: grid;&lt;br&gt;
  grid-template-columns: 1fr 1fr;&lt;br&gt;
  gap: 20px;&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Our goal is to show/hide the Facts card description [&lt;em&gt;div tag with a class of faq-desp&lt;/em&gt;] by clicking on the card header [&lt;em&gt;header tag with a class of faq-header&lt;/em&gt;]&lt;/p&gt;

&lt;p&gt;Firstly we will hide the Facts card description using the CSS property height: 0px and overflow: hidden like below:&lt;br&gt;
&lt;code&gt;.faq-desp {&lt;br&gt;
  overflow: hidden;&lt;br&gt;
  height: 0px;&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now the exciting part is to toggle (show/hide) the description using Javascript. Code snippet below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const faqItems = document.querySelectorAll(".faq-item");

faqItems.forEach((faq) =&amp;gt; {
  const faqHeader = faq.querySelector(".faq-header");

  faqHeader.addEventListener("click", () =&amp;gt; {
    const openFaq = document.querySelector(".faq-open");

    toogleFaq(faq);

    if (openFaq &amp;amp;&amp;amp; openFaq !== faq) {
      toogleFaq(openFaq);
    }
  });
});

const toogleFaq = (faq) =&amp;gt; {
  const faqDescription = faq.querySelector(".faq-desp");

  if (faq.classList.contains("faq-open")) {
    faqDescription.removeAttribute("style");
    faq.classList.remove("faq-open");
  } else {
    faqDescription.style.height = faqDescription.scrollHeight + "px";
    faq.classList.add("faq-open");
  }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Steps to achieve the goal:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Select all Facts cards using querySelectorAll&lt;/li&gt;
&lt;li&gt;Loop over each card using forEach and select the card header&lt;/li&gt;
&lt;li&gt;Add a click event listener on the header and call the toggle function (it shows/hide the fact card description) with the card as an argument in the callback function of the event listener&lt;/li&gt;
&lt;li&gt;The toggle function selects the card description and adds an inline style of height which is equal to the scrollHeight. [&lt;em&gt;The scrollHeight property returns the height of an element including padding, but excluding borders, scrollbars, or margins&lt;/em&gt;]&lt;/li&gt;
&lt;li&gt;We have achieved our goal. Rest I have added additional styles to make the accordion look presentable&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Please find the complete code in the codesandbox here: &lt;a href="https://codesandbox.io/s/javascript-accordion-7hemqb"&gt;Code Sandbox&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>html</category>
      <category>css</category>
      <category>accordion</category>
    </item>
  </channel>
</rss>
