COMP 1537 - Week 8 - DOM Creating, and Events
COMP 1537 - Week 8 - DOM Creating, and Events
The DOM is the data representation within the runtime environment that represents
HTML structure
Where structure could be:
An entire HTML document (including, but not limited to the current loaded document)
HTML snippets that are in-memory
The W3C DOM:
Has a specified set of programming calls
memorize
Referred to as an Application Programming Interface (API)
Has programming language bindings for Java, PHP, C#, C++, ECMA Script (so JavaScript,
ActionScript)
A model driven parser (tree based)
Reads the entire XML (HTML in our case) document into memory
And load the XML elements, attributes, comments, namespaces, etc., into memory
Where each element item is an in-memory object
This creates an in-memory hierarchical representation of the document
"The W3C Document Object Model (DOM) is a platform and language-neutral interface that allows programs and scripts to
dynamically access and update the content, structure, and style of a document." Copyright Ⓒ 2022, Arron Ferguson
DOM CONCEPTS (1/4)
The DOM treats every item in an XML (HTML too) document as a node (this is the
super type)
The node super type has several sub types:
Elements, text, PI, namespace, document, comment, entity, attribute, and a few others
Each of the node types is represented in memory using programming language objects
Allowing the manipulation (add/edit/delete) of each type in a concise & consistent manner
NodeList is the type returned when asking for a node's child nodes (could be
elements, text, etc.)
The NodeList object is a collection of child nodes
List of types for DOM
So, how does the W3C DOM see an XML/HTML document?
Attributes are not considered “child nodes” of the their surrounding element
They are considered “properties” of the element they are contained within
Attributes do have text nodes
Or rather they have a text node
Since only one the API call doesn't require a call to child nodes
The document (which is of type document) object has many properties and many
methods as well (see DOM-creating.html)
Some properties that are commonly used:
body, forms, images, links, scripts
Some methods that are commonly used:
createTextNode(), getElementById(), createAttribute(), getElementsByClassName(),
getElementsByName(), getElementsByTagName(), querySelector(), querySelectorAll(),
appendChild(), setAttribute(), replaceChild(), createElement() – to name a few!
There are event types for various different things that happen
Mouse events – mouse click, mouse over, etc.
Key events – key pressed, key down, key up, etc.
Window events – page finished loading, etc.
Form events – user attempted to submit, etc.
Clipboard events – user copied/pasted, etc.
Media events – media data is loaded, etc.
For more on each of the event types, see:
https://developer.mozilla.org/en-US/docs/Web/Events
Element addEventListener:
element.addEventListener(event, function,
useCapture);
Where:
Element is a DOM element (e.g., div)
Event is an event (e.g., click)
Function is a defined function, could be a(n):
Anonymous function expression
Function declaration
useCapture: true or false
Capture is at the beginning of the event
Bubble is at the end of the event
https://developer.mozilla.org/en-
US/docs/Web/API/Document_Object_Model/Introduction
https://developer.mozilla.org/en-US/docs/Web/API/Document_object_model
https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Events
https://developer.mozilla.org/en-US/docs/Web/Events
https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener
https://www.w3.org/TR/2003/NOTE-DOM-Level-3-Events-20031107/events.html#Events-
phases