I'll explain what happens:
window.addEventListener("click", sliderButtonsClick);
When loading your page, a click listener is registered for the entire window, which will only be executed when clicking:
function sliderButtonsClick(){
document.getElementById('all').onclick=function(){
}
}
And I do not mean to execute the click function for the all
button if you do not assign the click event to the button to listen to the next click you click on it.
That's the reason why on your first click the button does not do anything, since it still does not have any assigned events until the first click on window
.
To work with your first click, what you have to do is change your function as follows:
function sliderButtonsClick(){
document.getElementById('content-all').style.opacity=1;
}
document.getElementById('all').addEventListener("click", sliderButtonsClick);
#all{
opacity: 1;
}
#content-all{
opacity: 0;
}
<button id='all'>
Clic
</button>
<div id='content-all'>
Prueba
</div>
If you notice here I'm skipping the add the event on the window and do it directly on the button to avoid giving the second click for the function to run as you need it.