function hideAllButtons(button) {
button.querySelectorAll(".play, .pause, .speaker").forEach(hide);
}
Is changed to this:
function hideAllButtons(button) {
const buttons = button.querySelectorAll(".play, .pause, .speaker");
for (let i = 0; i < buttons.length; i += 1) {
hide(buttons[i]);
}
}
function hideAllButtons(button) {
const buttons = button.querySelectorAll(".play, .pause, .speaker");
for (let i = 0; i < buttons.length; i += 1) {
hide(buttons[i]);
}
}
Itâs here, it makes no difference if you save that string as a variable and use the variable or if you pass the string in directly, itâs exactly the same. Itâs not set up differently, itâs identical.
function hideAllButtons(button) {
const buttonSelectors = ".play, .pause, .speaker";
for (let i = 0; i < buttons.length; i += 1) {
hide(buttons[i]);
}
}
I think if you are having issues here you need to look go back to the very, very basics of JavaScript - ie how do you declare a variable, what is a variable, what is a string.
Edit:
This doesnât work:
function hideAllButtons(button) {
const buttonSelectors = ".play, .pause, .speaker";
for (let i = 0; i < buttons.length; i += 1) {
hide(buttons[i]);
}
}
Because youâve taken the perfectly fine working solution you had and deleted the part that made it work (buttons, the collection of buttons from the querySelectorAll call, is no longer there, youâve removed it)
function hideAllButtons(button) {
const buttons = button.querySelectorAll(".play, .pause, .speaker");
for (let i = 0; i < buttons.length; i += 1) {
hide(buttons[i])
}
}
This is your code from the jsfiddle. Itâs already working and utilizes the for loop you keep asking about, so whatâs the problem?
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING) {
const temp = event.target.a.src;
for (let i = 0; i < players.length; i++) {
if (players[i].a.src != temp) players[i].pauseVideo();
}
}
}