Ask coding questions

← Back to all posts
HTML Reactive Toggle Settings
lolguineapig (26)

I have been trying to figure out a way to make a real reactive toggle settings. What I want to do is create a toggle switch that toggles music. Can anyone help me with this?

Thanks,
@lolguineapig (and @daSeaTurtle)

Answered by Coder100 (18872) [earned 5 cycles]
View Answer
Comments
hotnewtop
Coder100 (18872)

assuming you have the css down, and all you need is the js, here is an example.

Given this HTML:

<button class="toggle">Toggle me</button>

we have:

document.querySelector(".toggle").addEventListener("click", () => {
  // whatever code you need to turn on and off the music ( maybe .play() and .stop()? )
});
lolguineapig (26)

@Coder100
Thank you so much for this, what I was thinking is that instead of a button, I was using an <input type="checkbox">, with css that looked like the image below

Coder100 (18872)

yeah, its pretty much the same thing @lolguineapig

Coder100 (18872)

i was just having the button as an example @lolguineapig