This?
function getButtonContainer (el) {
while (el.classList.contains("playButton") === false) {
el = el.parentNode;
}
return el;
}
This?
function getButtonContainer (el) {
while (el.classList.contains("playButton") === false) {
el = el.parentNode;
}
return el;
}
No, âplayButtonaâ is bad.
I thought you were going to show me a jsfiddle with it set up correctly?
Certainly not - that would remove all opportunities for learning to occur.
I will happily though help you to code this up yourself.
getPlay
getPause
means what?
The getPlay() function accepts a parameter of button, and returns the play icon inside of that button.
The getPause() works in a similar way.
I donât understand any of this.
show
hide
getButtonContainer
getPlay
getPause
showPlayButton
showPauseButton
pauseAllButtons
getAudio
isPlaying
playAudio
pauseAudio
togglePlayButton
playButtonClickHandler
What do I do next?
(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 togglePlayButton(button) {
var player = button.querySelector("audio");
var play = button.querySelector(".play");
var pause = button.querySelector(".pause");
player.volume = 1.0;
if (player.paused) {
hide(play);
show(pause);
player.play();
button.classList.add("active");
} else {
button.classList.remove("active");
show(play);
hide(pause);
player.pause();
}
}
function playButtonClickHandler(evt) {
var button = getButtonContainer(evt.target);
togglePlayButton(button);
}
var playButton = document.querySelector(".playButton");
playButton.addEventListener("click", playButtonClickHandler);
}());
You know these functions already:
Hereâs what these other ones do.
Then we have audio-related functions for the player itself
<audio>
elementAm I doing all this to accommodate this code?
Meaning, if I wasnât using this code, I wouldnât be making any changes.
I just want to make sure I have this correct, and weâll continue.
function togglePlayButton(button, audio) {
pauseAllButtons();
if (isPlaying(audio)) {
pausePlayer(audio);
} else {
playButton(button);
playPlayer(audio, button.getAttribute("data-audio"));
}
Nope, most of those functions make your existing code easier to understand and work with, as well.
Most of those functions arenât needed for the changes to occur, but without those functions the changes become much harder to understand.
Did I do this right?
function getButtonContainer(el) {
while (el.classList.contains("playButton") === false) {
el = el.parentNode;
}
return el;
}
itâs not possible to give an answer to that without seeing how itâs being used.
All I did was change the name.
Was I supposed to do that?
function getPlayButton(el) {
while (el.classList.contains("playButton1") === false) {
el = el.parentNode;
}
return el;
}
Now I can answer that itâs been done right.
Can you put together instructions on how to set up all the new stuff?
or, are instructions not needed?
Well the main code piece is:
function togglePlayButton(button, audio) {
pauseAllButtons();
if (isPlaying(audio)) {
pauseAudio(audio);
} else {
showPlayButton(button);
playAudio(audio, button.getAttribute("data-audio"));
}
}
We could modify your existing code so that it at a minimum, uses the functions that are shown here.
Is all of that, this?
getPlay -
gets a reference to the play icon element
getPause
- gets a reference to the pause icon element
showPlayButton
- shows the play icon, and hides the pause icon
showPauseButton
- shows the pause icon, and hides the play icon
pauseAllButtons
- finds all the pause icons and shows them all
Then we have audio-related functions for the player itself
getAudio
- gets a reference to the
No, itâs only about half of that.
The other half of the functions make the code easier to read.
For example, the pauseAudio line only has one line in the function, but because the playAudio function is required and more complex, having a matching pauseAudio function makes it easier to see that they are both directly related.