DOM Flashcards
1
Q
DOM
A
- DOM is the interface between your JavaScript and HTML + CSS
- The browser turns every HTML tag into a JS object that we can manipulate.
- document object model connects your JavaScript to HTML & CSS
- JavaScript objects that’s modeling your HTML elements
- Document is the root node where everything other object lives.
• each HTML element is modeled into JS objects behind the scene
2
Q
document.getElementByid()
A
• selects an element by id
3
Q
document.getElementsByClassName()
A
• selects an element by class
4
Q
document.getElementsByTagName()
A
• selects elements by tag name.. li, h1, etc
5
Q
document.querySelector()
A
- returns the first element that matches a given CSS style selector
- must use “#name” when selecting and id
- must use “.name” when selecting a class
- must use “name” when selecting an element.
6
Q
document.querySelectorAll()
A
• returns a list of elements that matches a given CSS-style selector
7
Q
style
A
• is a property to manipulate an elements style.
//select var tag = document.getElementById("highlight");
//Manipulate tag.style.color = "blue"; tag.style.border = "10px solid red"; etc etc
8
Q
classlist
A
- A read-only list that contains the classes for a given element. It is not an array
- better solution than style object
var tag = document.querySelector(“h1”);
//Add A class to the selected element tag.classList.add("another-class");
//remove a class tag.classList.remove("another-class");
//Toggle a class tag.classList.toggle("another-class");
9
Q
textContent
A
- Returns a string of all the text contained in a given element.
- treats everything as text. Even if passed text with tags in it the tags won’t render.
//select the <p> tag: var tag = document.querySelector("p");
//retrieve the textContent: tag.textContent //"This is great"
//alter the textContent: tag.textContent = "blah blah blah";</p>
10
Q
innerHTML
A
- similar to textContent, except it returns a string of all the HTML contained in a given element.
- will render the tags if tags are in the string.
11
Q
getAttribute()
setAttribute()
A
• used to read and write attributes like src or href
12
Q
addEventListener
A
• a method that adds a listener to a user event
element.addEventListener(type, functionToCall);
var button = document.querySelector("button"); button.addEventListener("click", function() { console.log("someone clicked the button!"); });