JavaScript - The Document Object Model Flashcards
The Document Object Model (DOM) provides a way to ____ and _____ the contents of a web page.
The Document Object Model (DOM) provides a way to access and alter the contents of a web page.
The DOM allows us to ___/____ or ___ elements, attributes and structure.
The DOM allows us to add/modify or remove elements, attributes and structure.
The _________ is the document that is referred to in “Document Object Model”. JavaScript uses specific terms to refer to parts (page objects) of the document.
The webpage is the document that is referred to in “Document Object Model”. JavaScript uses specific terms to refer to parts (page objects) of the document.
The DOM represents a document with a ____ _____. Each branch of the tree ends in a node, and each node contains objects. DOM methods allow programmatic _____ to the tree; with them you can change the document’s ______, ______, or ______. Nodes can also have event handlers attached to them; once an event is triggered, the event handlers get executed.
The DOM represents a document with a logical tree. Each branch of the tree ends in a node, and each node contains objects. DOM methods allow programmatic access to the tree; with them you can change the document’s structure, style, or content. Nodes can also have event handlers attached to them; once an event is triggered, the event handlers get executed.
Every document is made up of “_____”. There are different types of nodes, primarily, _____, ______ and _____.
Every document is made up of “nodes”. There are different types of nodes, primarily, element, attributes and text.
We can use these nodes to _____ specific parts of the document
We can use these nodes to target specific parts of the document
If you need to address the text of a paragraph, you need to target the _____ _____ of the paragraph NOT the _______ _______.
If you need to address the text of a paragraph, you need to target the text node of the paragraph NOT the paragraph itself.
Targeting elements nodes is used often. JavaScript can target elements easily via ______ and _____.
Targeting elements nodes is used often. JavaScript can target elements easily via classes and ids.
Example: //returns a node var node = document.getElementById("id");
//returns an element list of nodes var nodeList = document.getElementsByTagName("tagName");
//returns an element list of nodes (HTML5 only) var nodeList = document.getElementsByClassName("className");
With modern browsers, you are able to target elements using _____ _______.
With modern browsers, you are able to target elements using CSS selectors.
//returns the first node that matches the selector var node = document.querySelector(selectors);
//Returns a list of nodes that match the specified group of selectors var nodeList = document.querySelectorAll(selectors);
The query selector will only work for CSS selectors that the browser _______.
The query selector will only work for CSS selectors that the browser recognizes.
However, elements may be targeted relative to other elements;
//targets elements that are children of parentElement var nodeList = parentElement.getElementsByTagName("tagName");
Give an example of how to modify content:
//creates a new element node var newElement = document.createElement("elementType")
//creates a new text node var textNode = document.createTextNode("some text here");
//appends the node to the parent as its last child parentElement.appendChild(node);
//inserts a new element before the target element where both elements are children of parentElement parentElement.insertBefore(newElement, targetElement);
Creating all of your elements using JavaScript enables greater _______ of your application, including performance. Therefore, developers do not target elements as described above, they reference existing JavaScript variables. Effectively managing this technique leads into more advanced concepts. Modern frameworks apply this technique. More on that later.
Creating all of your elements using JavaScript enables greater control of your application, including performance. Therefore, developers do not target elements as described above, they reference existing JavaScript variables. Effectively managing this technique leads into more advanced concepts. Modern frameworks apply this technique. More on that later.
Give an example of how to modify attributes:
document.getElementById(‘linkId’).href = “http://www.google.com”;
myEl.getAttribute(“src”); myElemnt.setAttribute(“src”,”images/image2.jpg”); //created if doesnt exist.
Give an example of how to modify styles:
document.getElementById(“p2”).style.color=”blue”;