JavaScript Flashcards

1
Q

How do you create the illusion that you’re clicking from one page to another without actually changing pages?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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

A forEach loop with document.createElement().

forEach(answer => {
const button = document.createElement(‘button’)
button.innerText = answer.text;
answerButtonsElement.appendChild(button)
});

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What if I want to remove a long list of elements when I click one thing

A

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);
	}
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What if multiple elements have the same click event listener, but I need to know which one the user targetted?

A

Use e.target inside the function.

function selectAnswer(e) {
const selectedButton = e.target;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly