Build a Theme Switcher - Build a Theme Switcher

Tell us what’s happening: I am unable to pass step 28

Not sure how to clear the theme-message and body to be updated after the if statements for both li event listeners to try and clear step 28

This is the latest advice I received on socrates
“Your code adds the click listeners for the theme li elements inside the button’s toggle handler and uses hasAttribute / textContent.remove, which prevents the previous theme class and message from being cleared when a new theme is selected. Move the theme‑selection listeners out of the button click callback and update the body class and #theme-message using classList.remove and direct assignment to textContent, and try again.”

Your code so far

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Theme Switcher</title>
  <link rel="stylesheet" href="./styles.css">
</head>
<body>

  <button id = "theme-switcher-button" aria-haspopup = "true" aria-expanded = "false" aria-controls = "theme-dropdown">Switch Theme</button>
  <ul id = "theme-dropdown" role = "menu" aria-labelledby = "theme-switcher-button" hidden>
    <li id = "theme-light"role = "menuitem">light</li>
    <li id = "theme-dark" role = "menuitem">dark</li>
  </ul>
  <div id = "theme-message" aria-live = "polite"></div>
  <script src="./script.js"></script>
</body>
</html>
body {
  margin: 0;
  font-family: sans-serif;
  transition: background 0.3s, color 0.3s;
}

ul {
  margin: 0;
  padding: 0;
}

li {
  list-style-type: none;
}

#status {
  text-align: center;
  min-height: 20px;
}
let themes = [{name: "light", message: "Hi"},{name: "dark", message: "Bye"}]

let themeSwitcherBtn = document.getElementById("theme-switcher-button")
let themeDropdown = document.getElementById("theme-dropdown")
let lightTheme = document.getElementById("theme-light")
let darkTheme = document.getElementById("theme-dark")
themeSwitcherBtn.addEventListener("click", () => 
{ 

 if(themeDropdown.hasAttribute("hidden")) 
 {
  themeDropdown.removeAttribute("hidden")

  themeSwitcherBtn.setAttribute("aria-expanded","true")
 }
 else
 {
  themeSwitcherBtn.setAttribute("aria-expanded","false")
  themeDropdown.setAttribute("hidden", 'true')
    
 }
 
})

lightTheme.addEventListener("click", () => 
 {
  if(document.body.hasAttribute("theme-dark")){
    document.body.classList.remove("theme-dark")
    document.getElementById("theme-message").textContent = ""
  }
  document.body.classList.add("theme-light")
  document.getElementById("theme-message").textContent = themes[0].message
 })
 darkTheme.addEventListener("click", () => 
 {
  if(document.body.hasAttribute("theme-light")){
    document.body.classList.remove("theme-light")
    document.getElementById("theme-message").textContent = ""
  }
  document.body.classList.add("theme-dark")
  document.getElementById("theme-message").textContent = themes[1].message
  
 })

Lesson URL (copy - paste from your browser’s address bar)

Hi @d3911ready,

Please take a look at the example app in the instructions to see how your code should behave.

Currently, your code does not change the background color of the body element when a theme is selected, nor does it close the dropdown list after a selection.

Also, it is NOT best practice here to add an event listener to each hard-coded theme. What happens if you have four or five themes? Consider what you can do instead to dynamically discover the theme that is selected when the theme switcher button is clicked.

Happy coding