CoursesJavaScript TutorialQuery Selector All and classList
Lesson 32Beginner
4.9

JavaScript Tutorial logoQuery Selector All and classList

Continue your JavaScript Tutorial learning path with hands-on explanation, code practice, and quiz.

Course

JavaScript Tutorial

Estimated Time

8 min

Progress

44%

Track Position

32 / 73

Lesson Overview

Simple Explanation

DOM me multiple elements select karne ke liye querySelectorAll() use hota hai. classList se classes add, remove, toggle aur check kar sakte ho.

Code Explanation

Buttons ya cards par class toggle kar ke interactive UI banayi ja sakti hai.

Output Description

Selected elements par style class apply hogi aur active state change hogi.

Practice Exercise

Ek FAQ list banao jisme click par answer item active class toggle kare.

Extra Explanation

Why This Matters

Query Selector All and classList is core to dynamic behavior. Strong fundamentals here help you build forms, API flows, and interactive UI with confidence.

Real-World Workflow

Complex features are usually split into small functions, then verified through focused logging and tiny test cases.

Common Mistakes to Avoid

Avoid uncontrolled globals, weak error handling, and ignored async states. Define clear input-output expectations for each function.

Example + Live Practice

<!DOCTYPE html>
<html>
<body>
  <button class="tab">Tab 1</button>
  <button class="tab">Tab 2</button>

  <script>
    const tabs = document.querySelectorAll(".tab");

    tabs.forEach((tab) => {
      tab.addEventListener("click", () => {
        tab.classList.toggle("active");
        console.log(tab.textContent, tab.classList.contains("active"));
      });
    });
  </script>
</body>
</html>

Try It Yourself

Loading editor...

Test Your Knowledge

Quiz Coming Soon

Quiz for this lesson is not added yet.

Save Your Work

Lesson ke end par apna code save karein. Dashboard me aap kabhi bhi is saved code ko dobara dekh sakte hain.

Query Selector All and classList - dicodingschool