DOM Manipulation, Nodes and Events Flashcards
How do the css query selector methods work?
They retrieve a snapshot of a NodeList — that is, they aren’t updated every time they’re used.
How would you find all elements with a class of “block” using a query selector method?
var blocks = document.querySelectorAll(“.block”);
use a query selector to find one element by ID ‘myID’.
document.querySelector(“#myId”);
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)
someNode.classList.toggle(“myClass”);
What HTML5 property tells you if a document is loading or has loaded?
document.readyState; // “loading” or “complete”
How can you see if one node contains another?
someNode.contains( otherNode ).
What’s the difference between someNode.children and someNode.childNodes?
children are only elements; childNodes are any type of node (text, whitespace, comments)
How can you create an event handler for DOM Level 2 such that you can also remove it?
Use a function statement and refer to the variable name.
btn.addEventListener(“click”, funcName, false);
What is event.target vs event.currentTarget?
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)
What is event.stopPropagation() vs event.stopImmediatePropagation()?
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.
What is the event.type property? Why would you use it?
It’s the type of event (mouseover, click, etc). Useful for handling multiple events with one handler – if (event.type == “mouseover”) …
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?
mouseenter. Mouseover would fire on descendents.
What are the ways to get mouse coordinates on the browser screen and on the page?
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.
What is relatedTarget and why might you use it?
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 could you check if a given element, div, has a class of “block”?
div.classList.contains(“block”);