Traversing and Manipulating the DOM Flashcards

1
Q

ParentNode.children

A
  • The ParentNode.children read-only property returns a live HTMLCollection of child elements of the given object
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

variable i

A

short for “index”

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

Element.querySelector

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

listItems should be the children of the navigation unordered list. Fix it.

var listItems = navigation;

A

var listItems = navigation.children;

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

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;

A

var anchor = listItem.querySelector(“a”);

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

document.createElement

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Create a new div element and assign it to newDiv

var newDiv

A

var newDiv = document.createElement(“div”);

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

Create a new span element and assign it to newSpan

var newSpan

A

var newSpan = document.createElement(“span”);

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

Node.parentNode

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Node.appendChild

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Node.removeChild

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Remove the pleaseEnableParagraph from the body

A

body.removeChild(pleaseEnableParagraph);

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

Append the newParagraph to the body

A

body.appendChild(newParagraph);

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

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);

A

newParagraph.textContent = “JavaScript is enabled”;

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

Element.classList

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Use the classList property on the anchor to add a new class of “selected” to it

var anchor = document.querySelector(“a”);

A

anchor.classList.add(“selected”);

19
Q

Use classList to toggle the “live” class on the anchor element

var anchor = document.querySelector(“a”);

A

anchor.classList.toggle(“live”);

20
Q

EventTarget.addEventListener

A
  • the EventTarget.addEventListener() method registers the specified listener on the EventTargetit’s called on
  • it allows adding more than a single handler for an event

target.addEventListener(type, listener

  • type
    • a string representing the event type to listen for
  • listener
    • the object that receives a notification when an event of the specified type occurs
    • this must be an object implementing the EventListener interface, or simply a JavaScript function
21
Q

fix it so that both of the handlers trigger on “change” rather than just the last function assigned to onchange

navigationSelect.onchange = navigateToValue;
navigationSelect.onchange = sendAnalytics;
A

navigationSelect.addEventListener(“change”, navigateToValue);
navigationSelect.addEventListener(“change”, sendAnalytics);