I am not familiar with how this works, but this is what I have been told.
If anyone on here would like to give more input, or if there may be a better way to write this code, feel free to give as much information.
Is there a more simpler way to write this code?
Is there a better way to do this? A better way to write it?
Is any of it able to be improved?
Maybe it could be written using less code, I don’t know.
That could be done by adding a
data-container
attribute to each button which stores a class or an id of the container it should act upon. If you did that, you’d need a different class or id for each container (you already do that withplay1
andplay2
). Add adata-container
attribute to each button with its value as the corresponding container class (like for example:data-container="play1"
to the.playa
button anddata-container="play2"
to the.playb
button) and replacecover.previousElementSibling.querySelector(".container")
withdocument.getElementsByClassName(playButton.dataset.container)[0]
.
First Solution: https://jsfiddle.net/6vughped/
function showCover(playButton) {
hideAll(config.containers);
resetPage();
markAsPlayed(playButton);
const playButtonContainer = playButton.parentElement;
const container = document.getElementsByClassName(playButton.dataset.container)[0];
playButtonContainer.classList.add("active");
container.classList.add("active");
show(playButtonContainer);
show(container);
}
<div class="playButtonContainer with-curtain">
<button class="playa cover" type="button" data-container="play1"></button>
<button class="playb cover" type="button" data-container="play2"></button>
<button class="playc cover" type="button" data-container="play3"></button>
</div>
Second Solution: https://jsfiddle.net/xhe4bczt/
function showCover(playButton) {
hideAll(config.containers);
resetPage();
markAsPlayed(playButton);
const playButtonContainer = playButton.parentElement;
const playButtonIndex = Array.from(document.getElementsByClassName('cover')).findIndex(element => element == playButton)
const container = config.containers[playButtonIndex]
playButtonContainer.classList.add("active");
container.classList.add("active");
show(playButtonContainer);
show(container);
}
<div class="playButtonContainer with-curtain">
<button class="playa cover" type="button"></button>
<button class="playb cover" type="button"></button>
</div>
There are 2 other ways, one which maps buttons to containers by getting the last character of the class which starts with play, like in
playa
it’s a and inplay1
it’s 1, so it maps “a” to “1”, and the other by creating an object with keys as button classes mapping to values as container classes, like{ playa: 'play1' }
. Remember that for the first way, you can only have 26 containers named play1 to play26 and 26 buttons named playa to playz, and for the second way, whenever you add or remove a button and a container you need to update the object inside the arguments ofmanageCover.init
call inside theinit
function ofplayers
.
First Way: https://jsfiddle.net/uxkdgsc0/
function showCover(playButton) {
hideAll(config.containers);
resetPage();
markAsPlayed(playButton);
const playButtonContainer = playButton.parentElement;
const playButtonClasses = playButton.className.split(' ')
const playButtonPlayClass = playButtonClasses[playButtonClasses.findIndex(className => className.startsWith('play'))]
const containerPlayClass = 'play' + (playButtonPlayClass[4].charCodeAt() - 96)
const container = document.getElementsByClassName(containerPlayClass)[0]
playButtonContainer.classList.add("active");
container.classList.add("active");
show(playButtonContainer);
show(container);
}
<div class="playButtonContainer with-curtain">
<button class="playa cover" type="button"></button>
<button class="playb cover" type="button"></button>
</div>
Second Way: https://jsfiddle.net/c1qnrxak/
function showCover(playButton) {
hideAll(config.containers);
resetPage();
markAsPlayed(playButton);
const playButtonContainer = playButton.parentElement;
const playButtonClasses = playButton.className.split(' ')
let containerClass
for(var className of playButtonClasses) {
containerClass = config.playButtonsToContainers[className]
if (containerClass) break;
}
const container = document.getElementsByClassName(containerClass)[0]
playButtonContainer.classList.add("active");
container.classList.add("active");
show(playButtonContainer);
show(container);
}
function init(selectors) {
config.playButtonsToContainers = selectors.playButtonsToContainers;
}
function init() {
manageCover.init({
container: ".container",
playButton: ".cover",
playButtonsToContainers: {
playa: 'play1',
playb: 'play2',
playc: 'play3',
playd: 'play4',
playe: 'play5',
playf: 'play6',
playg: 'play7',
playh: 'play8',
plaei: 'play9'
}
});
<div class="playButtonContainer with-curtain">
<button class="playa cover" type="button"></button>
<button class="playb cover" type="button"></button>
</div>