JavaScript Flashcards
How do you create the illusion that you’re clicking from one page to another without actually changing pages?
The contents of both “pages” already exist in the HTML. Add a button with a click event listener, and a function that hides the contents of the first page and shows the contents of the next page.
Let’s say I have some data, like an array of objects, and I want to create a new HTML element for each object in that array. How do I do that?
A forEach loop with document.createElement().
forEach(answer => {
const button = document.createElement(‘button’)
button.innerText = answer.text;
answerButtonsElement.appendChild(button)
});
What if I want to remove a long list of elements when I click one thing
Create a reset state function that fires inside another function that’s activated during the click event and a while loop that loops over the elements in question. One example:
function resetState() {
while (element.firstChild) {
element.removeChild(element.firstChild); } }
What if multiple elements have the same click event listener, but I need to know which one the user targetted?
Use e.target inside the function.
function selectAnswer(e) {
const selectedButton = e.target;
}