DOM Manipulation, Nodes and Events Flashcards

1
Q

How do the css query selector methods work?

A

They retrieve a snapshot of a NodeList — that is, they aren’t updated every time they’re used.

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

How would you find all elements with a class of “block” using a query selector method?

A

var blocks = document.querySelectorAll(“.block”);

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

use a query selector to find one element by ID ‘myID’.

A

document.querySelector(“#myId”);

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

What’s a way to toggle a class on and off that doesn’t use jQuery and works only in the latest browsers? (all of them)

A

someNode.classList.toggle(“myClass”);

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

What HTML5 property tells you if a document is loading or has loaded?

A

document.readyState; // “loading” or “complete”

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

How can you see if one node contains another?

A

someNode.contains( otherNode ).

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

What’s the difference between someNode.children and someNode.childNodes?

A

children are only elements; childNodes are any type of node (text, whitespace, comments)

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

How can you create an event handler for DOM Level 2 such that you can also remove it?

A

Use a function statement and refer to the variable name.

btn.addEventListener(“click”, funcName, false);

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

What is event.target vs event.currentTarget?

A

target is the node that fired the event (like a menu item in a list).
currentTarget is the node to which the eventListener is attached (like the menu wrapper, the parent of the menu item)

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

What is event.stopPropagation() vs event.stopImmediatePropagation()?

A

stopPropagation() stops an event from bubbling or capturing (e.g,, so a listener on the body won’t respond to a button click).
stopImmediatePropagation() does this and also prevents other handlers from firing on the event.

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

What is the event.type property? Why would you use it?

A

It’s the type of event (mouseover, click, etc). Useful for handling multiple events with one handler – if (event.type == “mouseover”) …

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

If you want to know when mouse goes over a menu area but don’t want to know every time it goes over a descendent menu item, use what event type?

A

mouseenter. Mouseover would fire on descendents.

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

What are the ways to get mouse coordinates on the browser screen and on the page?

A

event.clientX event.clientY give coordinates on the screen (scrolling not taken into account).
event.pageX and .pageY take scrolling into account.
They are the same if page isn’t scrolled.

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

What is relatedTarget and why might you use it?

A

It’s an event property of the mouseover and mouseout events. Using it, you could tell if mouse left a menu item for another menu item, say, or if it left the menu altogether.
event.relatedTarget.classList.contains(“someclass”) will check if the related target has a class.

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

How could you check if a given element, div, has a class of “block”?

A

div.classList.contains(“block”);

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

How could you map, filter or otherwise iterate through a nodelist using an array function (e.g. filter or map)?

A

Array.prototype.forEach.call(myNodeList, func(el,i,list));

17
Q

What is event delegation?

A

It’s a way to optimize performance by not attaching too many event handlers. Since events bubble, a parent element (or even the document) can handle events, responding appropriately to each one based on event.target or other information.

18
Q

Using element traversal method, find the previous sibling to an element, myElement.

A

myElement.previousElementSibling()

19
Q

using element traversal method, find the last child of myElement that’s an element and change text to “Yo!”

A

myElement.lastElementChild.textContent = “Yo!”;

20
Q

Check if an element has a class; add, remove, or toggle a class.

A

element.classList.contains(‘someclass’); // add(), remove(), toggle();

21
Q

How can you get or set a data attribute called data-details on myElement

A

myElement.dataset.details = newValue; // no ‘data-‘

myElement.setAttribute(‘data-details’, newValue);

22
Q

What’s a good way to access the html tag?

A

document.documentElement;

23
Q

How can you remove a node from the DOM?

A
someNode.removeChild(childNode);
// This returns a reference to that node.
24
Q

What does someNode.normalize() do?

A

Combines adjacent text nodes into one and removes empty text nodes.

25
Q

What is a common way to retrieve radio buttons using a document method?

A

document.getElementsByName(“name”);

Radio buttons are usually named. Note: this returns an HTML collection.

26
Q

What are the five special collections that provide quick access to common parts of the document?

A

document.anchors (all a elements with a name attribute); document.applets; document.forms; document.images; document.links (all a elements with an href attribute).

27
Q

Name the methods that help find and change attributes on an HTML element.

A

getAttribute(attributeName), setAttribute(name, value), and removeAttribute(attrName); hasAttribute(attrName);

28
Q

If iterating a list of attributes for a given node called myDiv, how would you get each attribute and it’s value?

A

myDiv.attributes[i].nodeName; // “id”

myDiv.attributes[i].nodeValue; // “container”

29
Q

What are three ways to get or change text such as in a paragraph node?

A

textContent, innerText, node.firstChild.nodeValue, or innerHTML. These are properties, not functions. nodeValue fastest; MDN says textContent often has better performance than innerHTML.

30
Q

How do you use a document fragment node type and why?

A

Use it to reduce the number of times DOM needs to be re-rendered.
var frag = document.createDocumentFragment();
Add anything you want to frag then append it to the DOM when ready.

31
Q
What's wrong with this loop:
var blocks = document.getElementsByTagName("block");
for (var i = 0; i
A
blocks is a nodeList or HTML collection, which is "live" so it gets looked up every time it's accessed. Better to set a different variable and iterate over that:
var len = blocks.length.
32
Q

How can you find whether a node is ‘p’ or ‘div’ or something else?

A

someElement.nodeName;

33
Q

Summarize nodeValue, innerHTML, textContent, and innerText. How is each distinct?

A

nodeValue is faster than innerHTML but requires that a node already exists;
innerHTML parses content as HTML and takes longer.
textContent uses straight text, does not parse HTML, and is faster.
innerText Takes styles into consideration. It won’t get hidden text for instance.

34
Q

Replace an element (el) with an element (newEl)

A

el.parentNode.replaceChild(newChild, oldChild);

35
Q

Copy a node and all of it’s children

A

var newNode = someNode.cloneNode(true);