JavaScript DOM- DOM
Source: Accesibility - Udacity Front End Web Development Nanodegree
- Created with @ArtNerdnet
- What is the Document Object Model (DOM)?
- How the DOM gets created?
- How to access the DOM with JavaScript?
- a) The DOM
- b) Create the DOM
- c) Access the DOM
-
The DOM=
"Document Object Model"
-
It is
provided by the browser
-
It is a tree-like structure
representation of the HTML document
. -
It contains all nodes
relationship between elements
, (=the elementcontent
andproperties
). -
❗ NOTE: The DOM is NOT part of the JavaScript language. It ONLY reference the document object model in one place, the
"Global Object"
section. ECMAScript is the language specification JavaScript is based on.
- When you request a website, it responds with HTML:
- The received bytes run through a
parsing process
characters
===> (eg., start tag character, atribute)tags
===> it shows the HTML tags used (DOCTYPE, start tag, end tag, comments)tokens
===> it convert these tags into tokensnodes
===> it convert these tokens into nodes (HTML content and properties elements)DOM
===> it convert nodes into the DOM (a tree structure, that captures HTML content and properties and relationships between the nodes)
_ There are various ways to access the DOM with JavaScript:
- SELECT AN ELEMENT BY ID
document.getElementById();
If we ran the code above in the console, we wouldn't get anything, because we did not tell it the ID of any element to get! We need to pass a string to .getElementById() of the ID of the element that we want it to find and subsequently return to us:
document.getElementById('footer');
- SELECT MULTIPLE ELEMENTS
The next two DOM methods that we'll be looking at that both return multiple elements are:
.getElementsByClassName()
.getElementsByTagName()
Accessing Elements By Their Tag After looking at both .getElementById() and .getElementsByClassName(), the new .getElementsByTagName() method should seem quite easy on the eyes:
document.getElementsByTagName('p');
Important things to keep in mind:
- both methods use the
document object
- both
return multiple items
- the returned list=
__NOT__ an array[]
Node A Node acts like a blueprint containing all the data (properties) and the functionality (methods). This is called an interface. Based on the interface we know the properties and methods that are going to be applied to the individual items, which we call nodes.
ELEMENT INTERFACE The element interface inherits all properties AND methods from the Node. It is a descendent iself of the node, as well as its elements.
Example:
- The DOM is:
document object modal
, constructed from the browser - globally accessible by JavaScript code using the
document object
Creating Content with JavaScript Working with Browser Events Performance