DOM Manipulation Flashcards
What does DOM stand for?
Document Object Model
What does the following code do?
document.getElementById(‘aboutMe’);
This will select the element with the id of aboutMe from the HTML document.
What does the following code do?
document.querySelector(‘ol li’);
This will select the first li element inside of the first ol element.
What does the following code do?
document.querySelectorAll(‘p’);
This will select all paragraph elements within the HTML document.
What will the following code do?
let liTag = document.getElementsByTagName('li'); for(tag of liTag) { tag.style.fontFamily = 'cursive'; }
This will change the font family to cursive for each item with the li tag.
What are two methods that can change text on the DOM
- ) innerText : specifies the text content of the specified node
- ) innerHTML : specifies the HTML content of an element
What is the syntax for creating a new h1 element on a page?
document.createElement(‘h1’)
What will the following code display on the page?
let toDoTitle = document.createElement('h2'); let toDoDiv = document.getElementById('toDoDiv');
toDoDiv.appendChild(toDoTitle);
toDoTitle.innerText = ‘Needs to be done’;
This will place a new h2 element within the div with an id of “toDoDiv”. The h2 element text will be “Needs to be Done”
The only thing that will currently be displayed on the page is:
Needs to be Done
What document method can you use to select the first matching element within a document?
querySelector( )
Example Usage: document.querySelector('h1'); //this will select the first h1 element that appears in a document
What document method can you use to select all matching elements within a document?
querySelectorAll( )
Example Usage: document.querySelectorAll('li'); //this will select all elements within a document that have an li tag
What method can you use to select an element with its id?
getElementById( )
Example Usage: document.getElementById('animals'); //this would select the element with the id of 'animals'
Using this code to start, what would you need to do to add styling that changes the font color to blue and align the text to the center?
let para = document.querySelectorAll(‘p’);
let para = document.querySelectorAll(‘p’);
para. style.textAlign = ‘center’;
para. style.color = ‘blue’;