I am not sure what is wrong here since the code is working.
How do I fix it?
“203:12 TypeError: el.classList is undefined”
Code: Edit fiddle - JSFiddle - Code Playground
(function iife() {
"use strict";
function show(el) {
el.classList.remove("hide");
}
function hide(el) {
el.classList.add("hide");
}
function getButtonContainer(el) {
while (el.classList.contains("playButton") === false) {
el = el.parentNode;
}
return el;
}
function hideAllButtons(button) {
const buttons = button.querySelectorAll(".play, .pause, .speaker");
for (let i = 0; i < buttons.length; i++) {
hide(buttons[i]);
}
}
function getPlay(button) {
return button.querySelector(".play");
}
function getPause(button) {
return button.querySelector(".pause");
}
function getSpeaker(button) {
return button.querySelector(".speaker");
}
function showPlayButton(button) {
const play = getPlay(button);
hideAllButtons(button);
show(play);
}
function isPlaying(button) {
const play = getPlay(button);
return play.classList.contains("hide");
}
function pauseAllButtons() {
let buttons = document.querySelectorAll(".playButton");
for (let i = 0; i < buttons.length; i++) {
if (isPlaying(buttons[i])) {
showPlayButton(buttons[i]);
}
}
}
function showPauseButton(button) {
const pause = getPause(button);
pauseAllButtons();
hideAllButtons(button);
show(pause);
button.classList.add("activated");
}
function showSpeakerButton(button) {
const speaker = getSpeaker(button);
hideAllButtons(button);
show(speaker);
}
function getAudio() {
return document.querySelector("audio");
}
function playAudio(player, src) {
player.volume = 1.0;
if (player.getAttribute("src") !== src) {
player.setAttribute("src", src);
}
player.play();
}
function showButton(button, opts) {
if (opts.playing) {
showPlayButton(button);
} else {
showPauseButton(button);
}
}
function pauseAudio(player) {
player.pause();
}
function manageAudio(player, opts) {
if (opts.playing) {
pauseAudio(player);
} else {
playAudio(player, opts.src);
}
}
function playButton(button) {
const player = getAudio();
const playing = isPlaying(button);
showButton(button, {
playing
});
manageAudio(player, {
src: button.getAttribute("data-audio"),
playing
});
}
function showPause(button) {
if (isPlaying(button)) {
showPauseButton(button);
}
}
function showSpeaker(button) {
if (isPlaying(button)) {
showSpeakerButton(button);
}
}
function playButtonClickHandler(evt) {
const button = getButtonContainer(evt.target);
playButton(button);
}
function playButtonMouseoverHandler(evt) {
const button = getButtonContainer(evt.target);
showPause(button);
}
function playButtonMouseoutHandler(evt) {
const button = getButtonContainer(evt.target);
showSpeaker(button);
}
function initButton(selector) {
const wrapper = document.querySelector(selector);
wrapper.addEventListener("click", playButtonClickHandler);
wrapper.addEventListener("mouseover", playButtonMouseoverHandler);
wrapper.addEventListener("mouseout", playButtonMouseoutHandler);
}
initButton(".wrapc");
}());