Mid Term study DOM Flashcards
How to create an element node?
document.createElement(elementName)
Example:
let link = document.createElement(“a”);
link.href = “http://sheridancollege.ca”;
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”);
div.appendChild(a);
document.body.appendChild(div);
innerHTML vs createTextNode() vs textContent
Which one to use? and why?
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 to delete a HTML div with an id of mydiv?
let div = document.querySelector(“#mydiv”)
div.remove();