19
loading...
This website collects cookies to deliver better user experience
<button type="button" id="see-genres-button">See all genres and their descriptions</button>
<div id="genre-container">
let genresContainer = document.querySelector("#genre-container");
let genresButton = document.querySelector("#see-genres-button");
let showGenres = false;
let buttonStates = {
"See all genres and their descriptions":
"Hide the genres and their descriptions",
"Hide the genres and their descriptions":
"See all genres and their descriptions",
};
"See all genres and their descriptions"
showGenres = !showGenres;
. Then the genre info displays on page. The next time we click the button for displaying/hiding, showGenres = !showGenres;
will now take the showGenres variable which is now currently true and set showGenres to the opposite, thus making showGenres now equal to false. Because showGenres is now false, our else logic will run and hide the display of the genres-container. That is how this logic regarding the showGenres variable works to show the genres and hide the genres accordingly.let buttonStates = {
"See all genres and their descriptions":
"Hide the genres and their descriptions",
"Hide the genres and their descriptions":
"See all genres and their descriptions",
};
"See all genres and their descriptions"
from how our original html button is), and we are setting this innerText equal to the value of the key of that innerText string in the buttonStates object by doing buttonStates[genresButton.innerText]
. So on the first click of the button when it originally said "See all genres and their descriptions", after the click the button will now show the value of that string's key in the buttonStates object, so the button will then display "Hide the genres and their descriptions". On the next click of the button that now says "Hide the genres and their descriptions", the button after clicked will now then show the value of that string's key in the buttonStates object, so the button will then display "See all genres and their descriptions". Now the button text will correctly change from the show message to the hide message every time it is clicked.