🎭📂🪗 Building Modal, Dropdown, and Accordion with JavaScript

🎭📂🪗 Building Modal, Dropdown, and Accordion with JavaScript

“I clicked the button… something happened on the screen, but WHY?”

In the frontend world, there are some components that:

  • Stop the user 🛑
  • Force a decision 🤔
  • Bring order to chaos 🧘

Today, we’re building this UI Avengers trio:

  • Modal → Enters the stage with drama
  • Dropdown → Quiet but useful
  • Accordion → A bit obsessive about order, but right

All built with plain HTML + CSS + JavaScript
No frameworks. No excuses. Just learning 😎


🎭 1. MODAL – “STOP! You can’t leave without seeing this”

A modal is basically this:

“I’m normally hidden, but when someone clicks a button — BOOM 💥 — I jump onto the screen.”

🎯 Use Cases

  • Login / signup screens
  • Warnings
  • “I accept” drama
  • Campaign popups (yes… those annoying ones)

🧱 HTML – Let’s Build the Skeleton

<button id="openModal">Open Modal 🚀</button>

<div class="modal" id="modal">
  <div class="modal-content">
    <span class="close" id="closeModal">×</span>
    <h2>I’m a Modal 👋</h2>
    <p>Relax, I’m just here to inform you.</p>
  </div>
</div>

🧠 What’s happening here?

  • modal → Background overlay (the dark layer)
  • modal-content → White box (the main character)
  • × → The “close me please” scream

🎨 CSS – Stage Lights On

.modal {
  display: none; /* The most important line */
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
}

.modal-content {
  background: white;
  padding: 20px;
  width: 300px;
  margin: 100px auto;
  border-radius: 10px;
}

.close {
  cursor: pointer;
  font-size: 24px;
}

🧠 Golden tip:
Without display: none, the modal starts screaming the moment it’s born.


⚙️ JavaScript – Bringing It to Life

const modal = document.getElementById("modal");
const openBtn = document.getElementById("openModal");
const closeBtn = document.getElementById("closeModal");

openBtn.addEventListener("click", () => {
  modal.style.display = "block";
});

closeBtn.addEventListener("click", () => {
  modal.style.display = "none";
});

😄 JS logic in a nutshell:

“Show → Hide → Show again”
JavaScript loves minimalism.


💡 Modal Tips

  • You can add ESC key closing
  • Clicking the background can close it
  • classList.toggle makes cleaner code
  • Add animations = +100 charisma

📂 2. DROPDOWN – “It opens, but it behaves”

Dropdown means:

“Small menu, big responsibility.”


🧱 HTML

<button id="dropdownBtn">Menu ⬇️</button>

<ul id="dropdownMenu" class="dropdown">
  <li>Profile</li>
  <li>Settings</li>
  <li>Logout</li>
</ul>


🎨 CSS

.dropdown {
  display: none;
  list-style: none;
  padding: 10px;
  border: 1px solid #ccc;
}


⚙️ JavaScript – Toggle Magic

const btn = document.getElementById("dropdownBtn");
const menu = document.getElementById("dropdownMenu");

btn.addEventListener("click", () => {
  menu.style.display =
    menu.style.display === "block" ? "none" : "block";
});

🧠 The magic here:
On every click, JS asks one simple question:

“Am I open or closed?”


💡 Dropdown Tips

  • Clicking outside should close it (advanced level)
  • Touch sensitivity matters on mobile
  • Too many dropdowns = UX crying 😢

🪗 3. ACCORDION – “Order is everything”

Accordion means:

“Not everything can be open at the same time, my friend.”


🧱 HTML

<button class="accordion-btn">Question 1 ❓</button>
<div class="panel">Answer 1</div>

<button class="accordion-btn">Question 2 ❓</button>
<div class="panel">Answer 2</div>


🎨 CSS

.panel {
  display: none;
  padding: 10px;
  border: 1px solid #ddd;
}


⚙️ JavaScript – Find the Neighbor

const buttons = document.querySelectorAll(".accordion-btn");

buttons.forEach(btn => {
  btn.addEventListener("click", () => {
    const panel = btn.nextElementSibling;

    panel.style.display =
      panel.style.display === "block" ? "none" : "block";
  });
});

🧠 What is nextElementSibling?

“Give me the element right after me.”

The backbone of accordions 💪


💡 Accordion Tips

  • Want only one panel open at a time?
    • Close all panels first
  • Looks amazing with animations
  • The secret hero of FAQ pages

🧠 General Frontend Golden Rules

  • Use class toggling instead of display → clean code
  • JS = behavior, CSS = appearance
  • Small components = big impact
  • Code without UX thinking = garbage 😅

🎬 Final Words

Writing JavaScript UI is like this:

“You click a button…
and behind the scenes, a tiny logic machine starts spinning.”

Modal loves drama 🎭
Dropdown opens politely 📂
Accordion demands order 🪗

Comments

No comments yet. Why don’t you start the discussion?

Bir yanıt yazın

E-posta adresiniz yayınlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir