<?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>DEV Community: Ediomo Jose</title>
    <description>The latest articles on DEV Community by Ediomo Jose (@ediomojose).</description>
    <link>https://dev.to/ediomojose</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%2F812965%2Fbd251d2d-bed3-4393-88d2-09165f107aca.jpeg</url>
      <title>DEV Community: Ediomo Jose</title>
      <link>https://dev.to/ediomojose</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ediomojose"/>
    <language>en</language>
    <item>
      <title>Pop-up Modal: Using HTML, CSS, and JavaScript to Create a Modal.</title>
      <dc:creator>Ediomo Jose</dc:creator>
      <pubDate>Mon, 21 Aug 2023 11:44:45 +0000</pubDate>
      <link>https://dev.to/ediomojose/pop-up-modal-using-html-css-and-javascript-to-create-a-modal-19gd</link>
      <guid>https://dev.to/ediomojose/pop-up-modal-using-html-css-and-javascript-to-create-a-modal-19gd</guid>
      <description>&lt;h1&gt;
  
  
  An introduction to using modal pop-ups to make your website more engaging and interactive.
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;This article's main focus is on using the HTML dialog element to create a Modal Pop-up. To do this, we'll develop a subscription form that pops up a Modal success message after the form is submitted.&lt;/p&gt;

&lt;h3&gt;
  
  
  Prerequsite
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;JavaScript fundamentals (beginning level is acceptable; I'd go into great length to explain everything )&lt;/li&gt;
&lt;li&gt;You have a basic understanding of HTML and CSS and have used them to build at least one webpage.&lt;/li&gt;
&lt;li&gt;An editor for code (VS Code is suggested).&lt;/li&gt;
&lt;li&gt;Of course, a browser—we advise using Chrome or Firefox.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Table of content
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;What is a Modal?&lt;/li&gt;
&lt;li&gt;When to use a Modal.&lt;/li&gt;
&lt;li&gt;Exploring the HTML Dialog element.&lt;/li&gt;
&lt;li&gt;The open attribute&lt;/li&gt;
&lt;li&gt;Building our HTML Structure&lt;/li&gt;
&lt;li&gt;Building our form structure.&lt;/li&gt;
&lt;li&gt;Building our Modal Pop-up using the HTML Dialog element.&lt;/li&gt;
&lt;li&gt;Styling our form&lt;/li&gt;
&lt;li&gt;Styling our dialog&lt;/li&gt;
&lt;li&gt;Backdrop pseudo-element&lt;/li&gt;
&lt;li&gt;Adding JavaScript functionality to our elements.&lt;/li&gt;
&lt;li&gt;Javascript &lt;code&gt;show()&lt;/code&gt; and &lt;code&gt;showmodal()&lt;/code&gt; function.&lt;/li&gt;
&lt;li&gt;Javascript &lt;code&gt;close()&lt;/code&gt; function.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What is a Modal?
&lt;/h2&gt;

&lt;p&gt;A modal is a type of display that pops up as an overlay over the main page of a website, its appearance deactivates all the page content just like an alert. But the difference is that a modal needs to be interacted with to deactivate, it either comes with a dismiss button or is deactivated when the user clicks on the keyboard or the mouse.&lt;br&gt;
Modals do not allow users to interact with the web page when it appears. Aside from interactivity, modals allow for focusing increase in the user focus as it appears.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwd7we98l3kf55jhy5nc1.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwd7we98l3kf55jhy5nc1.gif" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  When to use a modal
&lt;/h2&gt;

&lt;p&gt;Modals can be used for a variety of purposes, which are alerts, warnings, confirmations,&lt;br&gt;
Forms,  media shows(Like videos and music), and an onboarding page for a multi-step procedure as an example.&lt;/p&gt;

&lt;h2&gt;
  
  
  Exploring the HTML dialog element
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;dialog&lt;/code&gt; element is a representation of a dialog box or other interactive web components, like a subwindow or an alert that can be dismissed. It is simple to develop an interactive modal thanks to the &lt;code&gt;dialog&lt;/code&gt; element. &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;dialog&amp;gt;

&amp;lt;/dialog&amp;gt;


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

&lt;/div&gt;
&lt;h2&gt;
  
  
  The open attribute -
&lt;/h2&gt;

&lt;p&gt;Without the &lt;code&gt;open&lt;/code&gt; attribute, the dialog element will be hidden from our browser by default. Additionally, it will allow us to see the dialog's content as we style it using CSS.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/dYQfIcryc28bS6132z/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/dYQfIcryc28bS6132z/giphy.gif" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  The &lt;code&gt;open&lt;/code&gt; attribute appears as follows:
&lt;/h3&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;dialog open&amp;gt;

&amp;lt;/dialog&amp;gt;


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

&lt;/div&gt;
&lt;h1&gt;
  
  
  Building our HTML structure
&lt;/h1&gt;
&lt;h2&gt;
  
  
  Building the form
&lt;/h2&gt;

&lt;p&gt;We will create a subscription form that asks for the user's email address before we can develop the structure of how our modal should appear. When the subscribe button is hit, a modal with a confirmation message will appear.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;section class="signup__container"&amp;gt;
        &amp;lt;picture&amp;gt;
          &amp;lt;source
            media="(min-width:700px)"
            srcset="./assets/images/illustration-sign-up desktop.svg"
            class="desktop__img"
          /&amp;gt;
          &amp;lt;img
          src="./assets/images/illustration-sign-up-mobile.svg"
          alt=""
          srcset=""
          class="mobile__img"
        /&amp;gt;
        &amp;lt;/picture&amp;gt;
        &amp;lt;form method="dialog"&amp;gt;
          &amp;lt;h1 class="form__header"&amp;gt;Stay updated!&amp;lt;/h1&amp;gt;
          &amp;lt;p class="form__paragraph"&amp;gt;
            Join 60,000+ product managers receiving monthly updates on:
          &amp;lt;/p&amp;gt;
          &amp;lt;ul class="outline"&amp;gt;
            &amp;lt;li class="outline__text"&amp;gt;
              Product discovery and building what matters
            &amp;lt;/li&amp;gt;
            &amp;lt;li class="outline__text"&amp;gt;
              Measuring to ensure updates are a success
            &amp;lt;/li&amp;gt;
            &amp;lt;li class="outline__text"&amp;gt;And much more!&amp;lt;/li&amp;gt;
          &amp;lt;/ul&amp;gt;
          &amp;lt;div class="label__error__text"&amp;gt;
            &amp;lt;label for="email__address"&amp;gt;Email Address&amp;lt;/label&amp;gt;
            &amp;lt;span class="error__state__text"&amp;gt;&amp;lt;/span&amp;gt;
          &amp;lt;/div&amp;gt;

          &amp;lt;input
            type="email"
            name="email"
            id="email__address"
            placeholder="email@company.com"
          /&amp;gt;
          &amp;lt;button class="form__submit" type="submit"&amp;gt;Subscribe to monthly newsletter&amp;lt;/button&amp;gt;
        &amp;lt;/form&amp;gt;
&amp;lt;/section&amp;gt;
    &amp;lt;!-- Sign-up form end --&amp;gt;


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

&lt;/div&gt;

&lt;p&gt;The form &lt;code&gt;method&lt;/code&gt; is &lt;code&gt;dialog&lt;/code&gt;, which is a crucial point to keep in mind. &lt;/p&gt;

&lt;h2&gt;
  
  
  Building the modal
&lt;/h2&gt;

&lt;p&gt;As demonstrated in the code below, the &lt;code&gt;dialog&lt;/code&gt; element must be used to create the modal.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;dialog&amp;gt;

&amp;lt;dialog/&amp;gt;


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

&lt;/div&gt;

&lt;p&gt;We must use the &lt;code&gt;open&lt;/code&gt; attribute, which was discussed at the beginning of this article, to see the result of what our &lt;code&gt;dialog&lt;/code&gt; structure will look like. Remember to remove the &lt;code&gt;open&lt;/code&gt; attribute once you're finished because adding it only serves to show the developer how the modal would appear on his browser.&lt;/p&gt;

&lt;h1&gt;
  
  
  Code  breakdown
&lt;/h1&gt;

&lt;p&gt;If you carefully go through the code, you will notice that we created a &lt;code&gt;div&lt;/code&gt; with the class &lt;code&gt;success__msg__container&lt;/code&gt;, which contains the entire content of our success message. If I give the &lt;code&gt;dialog&lt;/code&gt; element a display grid property directly, we will have defeated the purpose of a &lt;code&gt;dialog&lt;/code&gt; because the &lt;code&gt;dialog&lt;/code&gt; would no longer disappear from view even if the &lt;code&gt;open&lt;/code&gt; attribute was removed. I took this step because I want to give a display grid property to the &lt;code&gt;sccess__msg__container&lt;/code&gt;.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;dialog class="success__msg"&amp;gt;
      &amp;lt;div class="success__msg__container"&amp;gt;
        &amp;lt;img src="./assets/images/icon-success.svg" alt="" /&amp;gt;
        &amp;lt;h2 class="success__msg__head"&amp;gt;
          Thanks for subscribing!
        &amp;lt;/h2&amp;gt;
        &amp;lt;p class="success__note"&amp;gt;
          A confirmation email has been sent to
          &amp;lt;span id="user__email__address"&amp;gt;&amp;lt;/span&amp;gt;. Please open
          it and click the button inside to confirm your subscription.
        &amp;lt;/p&amp;gt;
        &amp;lt;button type="button" title="dismiss" class="dismiss__btn"&amp;gt;Dismiss message&amp;lt;/button&amp;gt;
      &amp;lt;/div&amp;gt;
&amp;lt;/dialog&amp;gt;


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

&lt;/div&gt;

&lt;p&gt;Also, take note that we added a &lt;code&gt;button&lt;/code&gt; after adding all the content that is required and that we wish to display. The modal will be closed when this &lt;code&gt;button&lt;/code&gt; is clicked.&lt;/p&gt;

&lt;h2&gt;
  
  
  Finally, here is the structure of our HTML code.
&lt;/h2&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;
  &amp;lt;head&amp;gt;
    &amp;lt;meta charset="UTF-8" /&amp;gt;
    &amp;lt;meta name="viewport" content="width=device-width, initial-scale=1.0" /&amp;gt;
    &amp;lt;title&amp;gt;Newsletter sign-up form with success message&amp;lt;/title&amp;gt;
    &amp;lt;link rel="stylesheet" href="style.css" /&amp;gt;
    &amp;lt;script src="./script.js" defer&amp;gt;&amp;lt;/script&amp;gt;
  &amp;lt;/head&amp;gt;
  &amp;lt;body&amp;gt;
    &amp;lt;!-- Sign-up form start --&amp;gt;


      &amp;lt;section class="signup__container"&amp;gt;
        &amp;lt;picture&amp;gt;
          &amp;lt;source
            media="(min-width:700px)"
            srcset="./assets/images/illustration-sign-up-desktop.svg"
            class="desktop__img"
          /&amp;gt;
          &amp;lt;img
          src="./assets/images/illustration-sign-up-mobile.svg"
          alt=""
          srcset=""
          class="mobile__img"
        /&amp;gt;
        &amp;lt;/picture&amp;gt;
        &amp;lt;form method="dialog"&amp;gt;
          &amp;lt;h1 class="form__header"&amp;gt;Stay updated!&amp;lt;/h1&amp;gt;
          &amp;lt;p class="form__paragraph"&amp;gt;
            Join 60,000+ product managers receiving monthly updates on:
          &amp;lt;/p&amp;gt;
          &amp;lt;ul class="outline"&amp;gt;
            &amp;lt;li class="outline__text"&amp;gt;
              Product discovery and building what matters
            &amp;lt;/li&amp;gt;
            &amp;lt;li class="outline__text"&amp;gt;
              Measuring to ensure updates are a success
            &amp;lt;/li&amp;gt;
            &amp;lt;li class="outline__text"&amp;gt;And much more!&amp;lt;/li&amp;gt;
          &amp;lt;/ul&amp;gt;
          &amp;lt;div class="label__error__text"&amp;gt;
            &amp;lt;label for="email__address"&amp;gt;Email Address&amp;lt;/label&amp;gt;
            &amp;lt;span class="error__state__text"&amp;gt;&amp;lt;/span&amp;gt;
          &amp;lt;/div&amp;gt;

          &amp;lt;input
            type="email"
            name="email"
            id="email__address"
            placeholder="email@company.com"
          /&amp;gt;
          &amp;lt;button class="form__submit" type="submit"&amp;gt;Subscribe to monthly newsletter&amp;lt;/button&amp;gt;
        &amp;lt;/form&amp;gt;
      &amp;lt;/section&amp;gt;
    &amp;lt;!-- Sign-up form end --&amp;gt;

    &amp;lt;!-- Success message start --&amp;gt;
    &amp;lt;dialog class="success__msg"&amp;gt;
      &amp;lt;div class="success__msg__container"&amp;gt;
        &amp;lt;img src="./assets/images/icon-success.svg" alt="" /&amp;gt;
        &amp;lt;h2 class="success__msg__head"&amp;gt;
          Thanks for subscribing!
        &amp;lt;/h2&amp;gt;
        &amp;lt;p class="success__note"&amp;gt;
          A confirmation email has been sent to
          &amp;lt;span id="user__email__address"&amp;gt;&amp;lt;/span&amp;gt;. Please open
          it and click the button inside to confirm your subscription.
        &amp;lt;/p&amp;gt;
        &amp;lt;button type="button" title="dismiss" class="dismiss__btn"&amp;gt;Dismiss message&amp;lt;/button&amp;gt;
      &amp;lt;/div&amp;gt;

    &amp;lt;/dialog&amp;gt;
    &amp;lt;!-- Success message end --&amp;gt;
  &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;



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

&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Take note of the class name given to each of the elements.&lt;/em&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Styling our Form and our Dialog Element
&lt;/h1&gt;

&lt;p&gt;The first step is to link our CSS stylesheet to our HTML file. Since the modal is the main focus of this article, I created some basic styling for the form and its container. We also added some basic styling to our &lt;code&gt;dialog&lt;/code&gt;, which you can see in the code below.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

@import url("https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&amp;amp;display=swap");
@import url("https://fonts.googleapis.com/css2?family=Roboto+Condensed&amp;amp;display=swap");

:root {
  --sucesspage-list: url(./assets/images/icon-list.svg);
  --dark-slate-grey: hsl(234, 29%, 20%);
  --charcoal-grey: hsl(235, 18%, 26%);
  --grey: hsl(231, 7%, 60%);
  --tomato: hsl(4, 100%, 67%);
  --white: hsl(0, 0%, 100%);
}

html {
  scroll-behavior: smooth;
}

*,
*::before,
*::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

img {
  max-width: 100%;
}

body {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  font-family: "Roboto", sans-serif;
  font-size: 1rem;
  background-color: var(--white);
}

.signup__container {
  display: grid;
  grid-template-columns: repeat(1, 1fr);
  grid-auto-flow: dense;
  border: none;
  border-radius: 0.5rem;
  margin: 2rem 1.2rem;
  box-shadow: 1px 2px 5px var(--grey);
}

.mobile__img {
  border-radius: 10px;
}

form {
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 1.5rem 1rem;
  gap: 1rem;
  color: var(--dark-slate-grey);
}

.form__header {
  font-size: 2em;
}

.form__paragraph {
  font-size: 0.9em;
  margin-bottom: 0.8rem;
}

.outline {
  list-style: none;
  font-size: 0.9em;
}

.outline__text {
  margin-bottom: 1rem;
  display: flex;
  gap: 1rem;
}

.outline__text::before {
  content: var(--sucesspage-list);
}

.label__error__text {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

label {
  font-weight: bold;
  font-size: 0.8em;
  color: var(--dark-slate-grey);
}

.error__state__text {
  font-weight: bold;
  font-size: 0.8em;
  color: red;
}

input {
  padding: 0.7rem;
  font-size: 1em;
  font-family: "Roboto Condensed", sans-serif;
  border-radius: 0.4rem;
  border: 1px solid var(--grey);
}

form button {
  padding: 1.2rem;
  margin-bottom: 2rem;
  background: var(--dark-slate-grey);
  color: var(--white);
  font-family: "Roboto", sans-serif;
  font-weight: 700;
  outline: none;
  border: none;
  border-radius: 0.4rem;
  cursor: pointer;
  box-shadow: 1px 2px 5px var(--grey);
  transition: all 5s ease;
}

form button:hover{
  background: linear-gradient(0.25turn,#e04313, hsl(4, 100%, 67%));
}

.success__msg {
  margin: auto;
  height: 70vh;
  width: 100%;
  padding: 3rem 2rem;
  border: none;
  border-radius: 1rem;
}

dialog::backdrop {
  background-color: hsl(234, 29%, 20%);
}

.success__msg__container {
  display: grid;
  grid-template-columns: repeat(1, 1fr);
  gap: 2rem;

}

.success__msg__head {
  font-size: 2.5em;
  color: var(--dark-slate-grey);
}

.success__note {
  color: var(--dark-slate-grey);
  line-height: 1.5rem;
}

#user__email__address {
  font-weight: 700;
}

.dismiss__btn {
  padding: 0.9rem;
  background: var(--dark-slate-grey);
  color: var(--white);
  font-family: "Roboto", sans-serif;
  font-weight: 700;
  outline: none;
  border: none;
  border-radius: 0.4rem;
  cursor: pointer;
  box-shadow: 1px 2px 5px var(--grey);
  transition: all 0.5s ease-in-out;
}

.dismiss__btn:hover {
  background: linear-gradient(0.25turn,#e04313, hsl(4, 100%, 67%));

}

/* Js class */

.error__state {
  border: 2px solid red;
}

.error__placeholder::placeholder {
  color: red;
}

.input__background {
  background-color: rgba(219, 26, 26, 0.1);
}

.body__position {
  position: fixed;
}

@media (width &amp;gt;= 600px) {
  body {
    margin: auto;
  }

  .signup__container {
    grid-template-columns: repeat(2, 1fr);
    margin: 0;
    width: 80%;
    border-radius: 1rem;
  }

  form button {
    margin: 0;
  }

  picture {
    grid-column: 2;
    padding: 1rem;
    display: flex;
  }

  .success__msg {
    width: 50%;
    height: 45vh;
    padding: 2rem;
  }

  .success__msg__container {
    gap: 2rem;
  }
}

@media (width &amp;gt;= 1024px) {
  .signup__container {
    width: 50%;
  }

  .success__msg {
    width: 30%;
    min-height: 70vh;
  }

  .success__msg__container {
    gap: 1rem;
  }
}

@media (width &amp;gt;= 1800px) {
  .signup__container {
    width: 35%;
  }

  .success__msg {
    width: 20%;
    min-height: 50%;
    padding: 2rem;
  }
}


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

&lt;/div&gt;
&lt;h1&gt;
  
  
  Backdrop pseudo-element
&lt;/h1&gt;

&lt;p&gt;The area behind our &lt;code&gt;dialog&lt;/code&gt; is styled using the &lt;code&gt;::backdrop&lt;/code&gt; pseudo-element in this case, we gave it a &lt;code&gt;background color&lt;/code&gt;.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

dialog::backdrop {
  background-color: hsl(234, 29%, 20%);
}



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

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw9gow31ij58s7wtibj0w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw9gow31ij58s7wtibj0w.png" alt="Moda pop-up large screen"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F34m2sp7oorm9bfquev0i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F34m2sp7oorm9bfquev0i.png" alt="Modal pop-up mobile"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Adding JavaScript functionality to our elements.
&lt;/h1&gt;

&lt;p&gt;Moving on to where the magic happens, we'll get a detailed understanding of how the modal pop-up is triggered, but first, we'll run a few lines of code to authenticate the input field. Although there are other methods of form authentication, for the purposes of this article, we'll stick with this one.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

const pageBody = document.querySelector("body");
const openModalBtn = document.querySelector(".form__submit");
let emailInput = document.querySelector("#email__address");
let errorStateText = document.querySelector(".error__state__text");
const successMessage = document.querySelector(".success__msg");
const successMsgEmail = document.querySelector("#user__email__address");
const dismissSucessMsgBtn = document.querySelector(".dismiss__btn");

const emptyFieldMsg = "Input field can not be blank";

openModalBtn.addEventListener("click", function () {
  if (!emailInput.value) {
    emailInput.classList.add("error__state");
    emailInput.classList.add("error__placeholder");
    emailInput.classList.add("input__background");
    errorStateText.innerText = emptyFieldMsg;
  } else {
    successMessage.showModal();
    successMsgEmail.innerText = emailInput.value;
  }
});

dismissSucessMsgBtn.addEventListener("click", function () {
  successMessage.close();
});


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

&lt;/div&gt;

&lt;p&gt;To store elements in a variable, the &lt;code&gt;document.querySelector()&lt;/code&gt; method calls the appropriate element in our DOM. &lt;/p&gt;

&lt;p&gt;The body of our website is stored in the &lt;code&gt;pageBody&lt;/code&gt; variable. Why must we perform this? This is done because we want to make the page unscrollable by adding the &lt;code&gt;position:fixed&lt;/code&gt; property to the body when the Modal comes into view.&lt;/p&gt;

&lt;p&gt;The button that will cause the modal to populate is the subscribe button from our form, and it is stored in the &lt;code&gt;openModalBtn&lt;/code&gt; variable.&lt;/p&gt;

&lt;p&gt;The email input field is stored in the &lt;code&gt;emailInput&lt;/code&gt; variable.&lt;/p&gt;

&lt;p&gt;In our HTML, the span with the class of &lt;code&gt;error__state__text&lt;/code&gt; is stored in the &lt;code&gt;errorStateText&lt;/code&gt; variable. If the input field is left empty, this should display the error message.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;successMessage&lt;/code&gt; variable stores the dialog element which is the modal&lt;/p&gt;

&lt;p&gt;&lt;code&gt;successMsgEmail&lt;/code&gt; represents the empty span we included in our &lt;code&gt;success__note&lt;/code&gt; in our HTML, and it is intended to display the email address the user supplied in the form.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;dismissSuccessMsgBtn&lt;/code&gt; variable keeps track of the button in our modal, which closes the modal.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;emptyFieldMsg&lt;/code&gt; variable, which records the error message that will be displayed, is the last of the variables.&lt;/p&gt;

&lt;p&gt;Next, a &lt;code&gt;callBack function&lt;/code&gt; that monitors the &lt;code&gt;openModalBtn&lt;/code&gt; click event is launched using the &lt;code&gt;addEventListener&lt;/code&gt; method. We added a conditional statement to the code that checks whether &lt;code&gt;emailInput&lt;/code&gt; contains a value. If it doesn’t, the error states should be triggered; else, the success message should appear.&lt;/p&gt;

&lt;h1&gt;
  
  
  Javascript &lt;code&gt;show()&lt;/code&gt; and &lt;code&gt;showmodal()&lt;/code&gt; function.
&lt;/h1&gt;

&lt;p&gt;JavaScript includes two methods called &lt;code&gt;show()&lt;/code&gt; and &lt;code&gt;showModal()&lt;/code&gt;. You may specify exactly how you want your dialog element to function by utilizing the &lt;code&gt;show()&lt;/code&gt; and &lt;code&gt;showModal()&lt;/code&gt; methods because sometimes you want a genuine modal and other times you want more of a popup-style dialog. To display our modal in our code, we utilized the &lt;code&gt;showModal()&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;Simply use the &lt;code&gt;close()&lt;/code&gt; method to close a &lt;code&gt;dialog&lt;/code&gt; element. Therefore, we utilize the &lt;code&gt;addEventListener&lt;/code&gt; method to start a callBack function that watches for the &lt;code&gt;dismissSuccessBtn&lt;/code&gt; click event.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9qusc5wv73yr0avlhrwt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9qusc5wv73yr0avlhrwt.png" alt="Modal pop-up"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxb5t3y7z07nnjpo7iezh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxb5t3y7z07nnjpo7iezh.png" alt="Modal pop-up"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/Ediomojose/newsletter-sign-up-with-success-message-main" rel="noopener noreferrer"&gt;Source Code&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://newsletter-sign-up-with-success-message-main-omega.vercel.app/" rel="noopener noreferrer"&gt;Live project preview&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;Sharing knowledge gives it its full power. I will ask you to do well to spread this information if it has been useful to you, as I know it has. There may be someone out there who is having trouble or needs clarification incorporating this into Modal-pop.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>tutorial</category>
      <category>css</category>
    </item>
    <item>
      <title>Carousel: How to create a testimonial carousel with HTML, CSS, and Javascript.</title>
      <dc:creator>Ediomo Jose</dc:creator>
      <pubDate>Mon, 24 Jul 2023 12:52:30 +0000</pubDate>
      <link>https://dev.to/ediomojose/carousel-how-to-create-a-testimonial-carousel-with-html-css-and-javascript-43j6</link>
      <guid>https://dev.to/ediomojose/carousel-how-to-create-a-testimonial-carousel-with-html-css-and-javascript-43j6</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;When developing web applications, one of the most crucial considerations for developers is responsiveness and interactivity. I have some exciting news for you: the carousel is one of the ways we can make our site appear spectacular.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a carousel
&lt;/h2&gt;

&lt;p&gt;A carousel is a round platform containing model horses, cars, and other items that revolves around that children ride on at a funfair.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/wJQF23Gt0eEMS2Zcbb/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/wJQF23Gt0eEMS2Zcbb/giphy.gif" width="480" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the context of web development, a carousel is a multi-content format used to display photographs, videos, customer testimonials, and other content. Carousels allow you to display several photos, videos, or a combination of images and text in a single container block and provide navigation buttons for moving forward and backward, resulting in an engaging experience.&lt;br&gt;
The excitement that children have when riding on a merry-go-round can be compared to the joy that our website visitors experience when they encounter interactive features such as a carousel on our website or web application.&lt;/p&gt;
&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Knowledge of HTML&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Knowledge of CSS&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Knowledge of Javascript&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  HTML Setup: Building the foundation
&lt;/h2&gt;

&lt;p&gt;The initial step in this process is to create our HTML fragment for this project. Once we've finished creating our HTML boilerplate, the next step is to connect our document to our CSS and Script files. Once that's done, we're ready to go.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/YHvwPgwojjsFa/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/YHvwPgwojjsFa/giphy.gif" width="450" height="293"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Creating our carousel
&lt;/h3&gt;

&lt;p&gt;Once your external files have been properly linked. We may now proceed to develop an HTML carousel. To accomplish this, we will create the carousel parent container and give it the class name &lt;code&gt;carousel__container&lt;/code&gt;. We'll have four user testimonials in our carousel container, each with the class name &lt;code&gt;carousel__item&lt;/code&gt; associated to it. The initial carousel item will additionally have the class &lt;code&gt;carousel__item—selected&lt;/code&gt; assigned to it. Its new class will allow us to switch between chosen carousel items.&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;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;
&amp;lt;head&amp;gt;
    &amp;lt;meta charset="UTF-8"&amp;gt;
    &amp;lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&amp;gt;
    &amp;lt;title&amp;gt;Testimonial carousel&amp;lt;/title&amp;gt;
    &amp;lt;link rel="stylesheet" href="post.css"&amp;gt;
    &amp;lt;script src="./post.js" defer&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
    &amp;lt;aside class="testimonials"&amp;gt;
        &amp;lt;h2 class="testimonials__head"&amp;gt;What they've said&amp;lt;/h2&amp;gt;
        &amp;lt;div class="carousel__container"&amp;gt;
          &amp;lt;div class="carousel__item carousel__item--selected"&amp;gt;
            &amp;lt;img
              src="./images/avatar-anisha.png"
              alt="user__avatar"
              srcset=""
            /&amp;gt;
            &amp;lt;div class="testimonial__block"&amp;gt;
              &amp;lt;h4 class="name"&amp;gt;Anisha Li&amp;lt;/h4&amp;gt;
              &amp;lt;p class="testimonial__paragraph"&amp;gt;
                “Manage has supercharged our team’s workflow. The ability to
                maintain visibility on larger milestones at all times keeps
                everyone motivated.”
              &amp;lt;/p&amp;gt;
            &amp;lt;/div&amp;gt;
          &amp;lt;/div&amp;gt;

          &amp;lt;div class="carousel__item "&amp;gt;
            &amp;lt;img src="./images/avatar-ali.png" alt="user__avatar" srcset="" /&amp;gt;
            &amp;lt;div class="testimonial__block"&amp;gt;
              &amp;lt;h4 class="name"&amp;gt;Ali Bravo&amp;lt;/h4&amp;gt;
              &amp;lt;p class="testimonial__paragraph"&amp;gt;
                “We have been able to cancel so many other subscriptions since
                using Manage. There is no more cross-channel confusion and
                everyone is much more focused.”
              &amp;lt;/p&amp;gt;
            &amp;lt;/div&amp;gt;
          &amp;lt;/div&amp;gt;

          &amp;lt;div class="carousel__item"&amp;gt;
            &amp;lt;img
              src="./images/avatar-richard.png"
              alt="user__avatar"
              srcset=""
            /&amp;gt;
            &amp;lt;div class="testimonial__block"&amp;gt;
              &amp;lt;h4 class="name"&amp;gt;Richard Watts&amp;lt;/h4&amp;gt;
              &amp;lt;p class="testimonial__paragraph"&amp;gt;
                “Manage allows us to provide structure and process. It keeps us
                organized and focused. I can’t stop recommending them to
                everyone I talk to!”
              &amp;lt;/p&amp;gt;
            &amp;lt;/div&amp;gt;
          &amp;lt;/div&amp;gt;

          &amp;lt;div class="carousel__item"&amp;gt;
            &amp;lt;img
              src="./images/avatar-shanai.png"
              alt="user__avatar"
              srcset=""
            /&amp;gt;
            &amp;lt;div class="testimonial__block"&amp;gt;
              &amp;lt;h4 class="name"&amp;gt;Shanai Gough&amp;lt;/h4&amp;gt;
              &amp;lt;p class="testimonial__paragraph"&amp;gt;
                “Their software allows us to track, manage and collaborate on
                our projects from anywhere. It keeps the whole team in-sync
                without being intrusive.”
              &amp;lt;/p&amp;gt;
            &amp;lt;/div&amp;gt;
          &amp;lt;/div&amp;gt;
      &amp;lt;/aside&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I know you're wondering where the button that allows us to switch between carousel items is; don't worry, this will be implemented utilizing our javascript.&lt;/p&gt;

&lt;h2&gt;
  
  
  CSS: Styling carousel
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@import url("https://fonts.googleapis.com/css2?family=Be+Vietnam+Pro:wght@400;500;700&amp;amp;display=swap");

:root {
  --bright-red: hsl(12, 88%, 59%);
  --dark-blue: hsl(228, 39%, 23%);
  --dark-grayish-blue: hsl(227, 12%, 61%);
  --very-dark-blue: hsl(233, 12%, 13%);
  --very-pale-red: hsl(13, 100%, 96%);
  --very-light-grey: hsl(0, 0%, 98%);
  --font-vietnam: "Be Vietnam Pro";
}

*,
::before,
::after {
  margin: 0;
  padding: 0;
  list-style: none;
  box-sizing: border-box;
}

body {
  font-size: 0.875rem;
  background-color: var(--very-pale-red);
  scroll-behavior: smooth;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  font-family: var(--font-vietnam);
}

img {
  max-width: 100%;
}

.testimonials {
  display: grid;
  grid-template-columns: repeat(1, 1fr);
  place-items: center;
}

.testimonials__head {
  font-size: 2.3em;
  padding: 2rem 0;
  color: var(--dark-blue);
}

.carousel__container {
  position: relative;
  padding: 2rem;
}

.carousel__item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  margin: 2rem 0 4rem 0;
  position: relative;
  display: none;
}

.carousel__item--selected {
  display: flex;
}

img[alt="user__avatar"] {
  width: 25%;
  position: absolute;
  top: -30px;
}

.testimonial__block {
  background: var(--very-light-grey);
  padding: 5rem 1.5rem 1rem 1.5rem;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.name {
  font-size: 1.2em;
  color: var(--dark-blue);
}

.testimonial__paragraph {
  font-size: 1em;
  text-align: center;
  line-height: 1.7rem;
  padding: 1rem 0;
  color: var(--dark-grayish-blue);
}

.carousel__nav {
  width: 100%;
  padding: 20px 0;
  position: absolute;
  bottom: 0;
  left: 0;
  text-align: center;
}

.carousel__btn {
  width: 10px;
  height: 10px;
  display: inline-block;
  border-radius: 50%;
  border: 1.5px solid var(--bright-red);
  background: transparent;
  cursor: pointer;
  margin: 0 5px;
  transition: 0.5s ease-in;
}

.carousel__btn--selected {
  background-color: var(--bright-red);
}

.carousel__action__btn {
  color: white;
  background-color: var(--bright-red);
  margin: 2rem 0;
  padding: 1rem 2.5rem;
  outline: none;
  border: none;
  border-radius: 2rem;
  cursor: pointer;
  box-shadow: 1px 10px 6px var(--very-pale-red);
}

@media (500px &amp;lt;= width &amp;lt;= 915px) {
  body {
    font-size: 0.9375rem;
  }

  .carousel__container {
    padding: 2rem 8rem;
  }

  img[alt="user__avatar"] {
    bottom: 2rem;
    width: 20%;
  }

  .testimonial__block {
    padding: 6rem 1.5rem 1rem 1.5rem;
  }
}

@media (width &amp;gt;= 916px) {
    body{
        font-size: 1.1rem;
    }

  .testimonials {
    margin: 5rem auto;
    max-width: 50%;
  }

  img[alt="user__avatar"] {
    width: 15%;
  }

  .testimonial__block {
    padding: 6rem 3rem 3rem 3rem;
    width: 100%;
  }
}

@media(width &amp;gt;= 1500px ){
  .testimonials {
    margin: 5rem auto;
    max-width: 30%;
  }

}

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

&lt;/div&gt;



&lt;p&gt;I'll do a good job of explaining some of the styling in our cascading style sheet (CSS). Take note we used a Mobile First approach in the styling.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;carousel__container&lt;/code&gt;: We gave the carousel container a relative position, which is critical for stacking carousel items.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;carousel__item&lt;/code&gt;: We also provided our carousel item a display none, of which hides the items. We did this because we wanted the selected carousel item to be visible.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;carousel__item__selected&lt;/code&gt;: This is where the class "carousel__item-selected" comes in, which has now been allocated the display flex attribute. After doing so, the carousel item with this class will be presented in view.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We also added the classes &lt;code&gt;carousel__nav&lt;/code&gt;, &lt;code&gt;carousel__btn&lt;/code&gt;, and &lt;code&gt;carousel__btn-selected&lt;/code&gt; to assist us in distinguishing between the buttons that are selected and those that are not. Remember that our JavaScript will construct these button elements.&lt;/p&gt;

&lt;h2&gt;
  
  
  JavaScript: Bring it to life
&lt;/h2&gt;

&lt;p&gt;You are welcome to where the magic happens, and I hope you have had a good time so far.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;document.querySelectorAll(".carousel__container").forEach((carousel) =&amp;gt; {
    const items = carousel.querySelectorAll(".carousel__item");
    const buttonHtml = Array.from(items, () =&amp;gt; {
      return `&amp;lt;span class="carousel__btn"&amp;gt;&amp;lt;/span&amp;gt;`;
    });
    carousel.insertAdjacentHTML(
      "beforeend",
      `&amp;lt;div class="carousel__nav"&amp;gt;${buttonHtml.join("")}&amp;lt;/div&amp;gt;`
    );

    const carouselBtns = carousel.querySelectorAll(".carousel__btn");

    carouselBtns.forEach((carouselBtn, i) =&amp;gt; {
      carouselBtn.addEventListener("click", () =&amp;gt; {
        // un-select all the items
        items.forEach((item) =&amp;gt;
          item.classList.remove("carousel__item--selected")
        );
        carouselBtns.forEach((carouselBtn) =&amp;gt;
          carouselBtn.classList.remove("carousel__btn--selected")
        );

        items[i].classList.add("carousel__item--selected");
        carouselBtn.classList.add("carousel__btn--selected");
      });
    });
  });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I'm going to go over the JavaScript code we just wrote in detail so we can understand exactly what we're doing.&lt;/p&gt;

&lt;p&gt;Using  &lt;code&gt;document.querySelector&lt;/code&gt; , we selected every item in our carousel, the method returns a nodeList, which we now loop through to loop over each of the items in the &lt;code&gt;carousel__container&lt;/code&gt; using &lt;code&gt;forEach()&lt;/code&gt;. It is worth noting that in our &lt;code&gt;forEach()&lt;/code&gt; method, the term &lt;code&gt;carousel&lt;/code&gt; that is found inside the parentheses symbolizes each item in the &lt;code&gt;carousel__container&lt;/code&gt;. Following that, we store each of these items in a variable called &lt;code&gt;items&lt;/code&gt;, specifying that items should be the variable that represents our &lt;code&gt;carousel__item&lt;/code&gt;; in essence, the &lt;code&gt;items&lt;/code&gt; variable now returns an array.&lt;/p&gt;

&lt;p&gt;Next, we made a variable called &lt;code&gt;buttonHtml&lt;/code&gt; and used the &lt;code&gt;Array.from()&lt;/code&gt; method to construct an array of buttons tied to each item, giving it the class &lt;code&gt;carousel__btn&lt;/code&gt;. We needed to include this navigation in our &lt;code&gt;carousel__container&lt;/code&gt;, so we used the &lt;code&gt;insertAdjacentHTML&lt;/code&gt; method to create a &lt;code&gt;div&lt;/code&gt; with the class &lt;code&gt;carousel__nav&lt;/code&gt;. Then, in the &lt;code&gt;div&lt;/code&gt;, we used the &lt;code&gt;.join()&lt;/code&gt;  method to add each of the buttons we made in the &lt;code&gt;carousel__nav&lt;/code&gt; section.&lt;/p&gt;

&lt;p&gt;We must make our buttons react to click events. If you look in your browser, you will notice buttons that have been stylized; this style was derived from the class name of our CSS file. We made a variable called &lt;code&gt;CarouselBtns&lt;/code&gt; and saved all of our buttons in it.&lt;/p&gt;

&lt;p&gt;We used the &lt;code&gt;forEach()&lt;/code&gt; loop to go through each of the buttons because the &lt;code&gt;carouselBtns&lt;/code&gt; variable returns an array in the pretenses we named each of the buttons &lt;code&gt;carouselBtn&lt;/code&gt; and the &lt;code&gt;i&lt;/code&gt; signifies their index. We provided a &lt;code&gt;click eventListener&lt;/code&gt; to each button within the &lt;code&gt;forEach()&lt;/code&gt; method code block.&lt;/p&gt;

&lt;p&gt;Following that, we took the items variable and looped through it with the &lt;code&gt;forEach()&lt;/code&gt; method to see if the class name &lt;code&gt;carousel__item—selected&lt;/code&gt; had been added and then removed it. We also used the &lt;code&gt;forEach()&lt;/code&gt; method on the c&lt;code&gt;orouselBtns&lt;/code&gt; to remove the class name &lt;code&gt;carousel__btn—selected&lt;/code&gt; if it had been added.&lt;/p&gt;

&lt;p&gt;Finally, we removed the selected class if it was present. The next step is to add those classes to both the &lt;code&gt;carouselBtn&lt;/code&gt; and the &lt;code&gt;items&lt;/code&gt; using the items index.&lt;/p&gt;

&lt;h2&gt;
  
  
  Links
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://testimonial-carousel-nine.vercel.app/"&gt;Testimonial Carousel Live Preview&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/Ediomojose/Testimonial-Carousel"&gt;Code GitHub Repository&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Congrats!!!!!!&lt;br&gt;
You have successfully created a carousel; I recommend that we go read up on some of the principles used in this implementation, particularly some of the JavaScript methods and functions.&lt;/p&gt;

&lt;h3&gt;
  
  
  Buy me a coffee☕
&lt;/h3&gt;

&lt;p&gt;You can show your appreciation for me by purchasing me a cup of coffee if you enjoyed and found value in my article. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.buymeacoffee.com/ediomojose"&gt;Buy me a cofee&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You should also like and follow for more stuff. Also, given this is my first technical article, please provide comments if you have any queries or see any errors.&lt;/p&gt;

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