DOM Manipulation and Events Flashcards

1
Q

What is the DOM?

A

The DOM (or Document Object Model) is a tree-like representation of the contents of a webpage - a tree of “nodes” with different relationships depending on how they’re arranged in the HTML document.

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

What does this do? element.querySelector(selector)

A

returns a reference to the first match of selector

Eg. const container = document.querySelector(“#container”);
// selects the #container div

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

What does it do?
element.querySelectorAll(selectors)

A

returns a “NodeList” containing references to all of the matches of the selectors.

It’s important to remember that when using querySelectorAll, the return value is not an array. It looks like an array, and it somewhat acts like an array, but it’s really a “NodeList”.

One solution, if problems arise, is to convert the NodeList into an array. You can do this with Array.from() or the spread operator.

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

DOM-
How do we create new element?

A

document.createElement(tagName, [options]) - creates a new element of tag type tagName. [options] in this case means you can add some optional parameters to the function.

const div = document.createElement(“div”);

This function does NOT put your new element into the DOM - it creates it in memory.

To place the element into the DOM, append element.

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

How to append elements?

A

2 ways:

parentNode.appendChild(childNode) - appends childNode as the last child of parentNode.

parentNode.insertBefore(newNode, referenceNode) - inserts newNode into parentNode before referenceNode.

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

DOM -
How to remove element?

A

parentNode.removeChild(child) - removes child from parentNode on the DOM and returns a reference to child.

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

How to alter elements?

A

When you have a reference to an element, you can use that reference to alter the element’s own properties.

// creates a new div referenced in the variable ‘div’

const div = document.createElement(“div”);

Then can add inline style:
// adds the indicated style rule to the element in the div variable
div.style.color = “blue”;

// adds several style rules
div.style.cssText = “color: blue; background: white;”;

// adds several style rules
div.setAttribute(“style”, “color: blue; background: white;”);

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

How to add new class?

A

// adds class “new” to your new div
div.classList.add(“new”);

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

How to remove class ?

A

// removes “new” class from div
div.classList.remove(“new”);

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

How to toggle class (add / remove)?

A

// if div doesn’t have class “active” then add it, or if it does, then remove it
div.classList.toggle(“active”);

It is often standard (and cleaner) to toggle a CSS style rather than adding and removing inline CSS.

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

How to create text node containing message and insert it in div?

A

// creates a text node containing “Hello World!” and inserts it in div
div.textContent = “Hello World!”;

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

What does the event target in Event Listener mean?

btn.addEventListener(“click”, function (e) {
console.log(e);
});

A

The e parameter in that callback function contains an object that references the event itself. Within that object you have access to many useful properties and methods (functions that live inside an object) such as which mouse button or key was pressed, or information about the event’s target - the DOM node that was clicked.

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

How to use e.target in Event Listener?

A

btn.addEventListener(“click”, function (e) {
e.target.style.background = “blue”;
});

Here, the event target which is the DOM node that was clicked will have the attribute style applied

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

How to add an event listener to all the buttons?

A

// we use the .forEach method to iterate through each button buttons.forEach((button) => { // and for each one we add a ‘click’ listener button.addEventListener(“click”, () => { alert(button.id); }); });

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