Traversing and Manipulating the DOM Flashcards
ParentNode.children
- The ParentNode.children read-only property returns a live HTMLCollection of child elements of the given object
variable i
short for “index”
Element.querySelector
- returns the first element that is a descendant of the element on which it is invoked that matches the specified group of selectors
- element and baseElement are element objects
- selectors is a group of selectors to match on
listItems should be the children of the navigation unordered list. Fix it.
var listItems = navigation;
var listItems = navigation.children;
We want to get the anchor (the a element) from inside the listItem. Use a method to traverse and select the anchor.
var anchor = listItem;
var anchor = listItem.querySelector(“a”);
document.createElement
- in an HTML document creates the specified HTML element or HTMLUnknownElement if the element is not known
- element is the created element object
- tagName is a string that specifies the type of element to be created
- the nodeName of the created element is initialized with the value of tagName
- don’t use qualified names (like “html:a”) with this method
Create a new div element and assign it to newDiv
var newDiv
var newDiv = document.createElement(“div”);
Create a new span element and assign it to newSpan
var newSpan
var newSpan = document.createElement(“span”);
Node.parentNode
- returns the parent of the specified node in the DOM tree
- parentNode is the parent of the current node
- the parent of an element is an Element node, a Document node, or a DocumentFragment node
Node.appendChild
- adds a node to the end of the list of children of a specified parent node. If the node already exists it is removed from current parent node, then added to new parent node
var child = element.appendChild(child);
- element is the parent element
- child is the node to append underneath element
- also returned
Node.removeChild
- removes a child node from the DOM. Returns removed node
- child is the child node to be removed from the DOM
- element is the parent node of child
- oldChild holds a reference to the removed child node. oldChild === child
Remove the pleaseEnableParagraph from the body
body.removeChild(pleaseEnableParagraph);
Append the newParagraph to the body
body.appendChild(newParagraph);
We’re removing a paragraph and adding a new one to the body. The newParagraph has no text in it. Modify the newParagraph element’s text to say “JavaScript is enabled”.
var body = document.body; var newParagraph = document.createElement("p"); var pleaseEnableParagraph = document.querySelector("#please\_enable");
body. removeChild(pleaseEnableParagraph);
body. appendChild(newParagraph);
newParagraph.textContent = “JavaScript is enabled”;
Element.classList
- classList returns a token list of the class attribute of the element
- add - Adds a class to an element’s list of classes
- remove - Removes a class from an element’s list of classes
- toggle - Toggles the existence of a class in an element’s list of classes
- See below about the optional second argument
- contains - Checks if an element’s list of classes contains a specific class