Mid Term study DOM Flashcards

1
Q

How to create an element node?

A

document.createElement(elementName)

Example:
let link = document.createElement(“a”);
link.href = “http://sheridancollege.ca”;

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

How would you append the <a> tag below to the div then append it all to the body?:</a>

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

A

div.appendChild(a);
document.body.appendChild(div);

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

innerHTML vs createTextNode() vs textContent

Which one to use? and why?

A

createTextNode(text) and textContent will escape any special characters, but innerHTML will not

innerHTML is less efficient because it forces the browser to re-parse or re-build the entire DOM tree every time it’s used

Therefore only use innerHTML if mandatory, opt to use createTextNode() or textContent.
Examples:
document.createTextNode(“Hello World”);

let p = document.createElement(“p”);
p.textContent = “Hello World”;

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

How to delete a HTML div with an id of mydiv?

A

let div = document.querySelector(“#mydiv”)
div.remove();

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