JavaScript HTML DOM EventListner
Event Listners are a core concept.
The addEventListner()
method
We can create an event listener that fires when a user clicks a button:
- Above, the
addEventListner()
method attaches an event handler to the specified element. TheaddEventListener()
method attaches an event handler to an element without overwriting existing event handlers. - So, we can have an event that has many event handlers, which makes sense. We can add many event handlers of the same type to one element, i.e. 2 “click” events. We like to use
addEventListner
since the JavaScript is separated from the HTML markup, ensuring better readability and maintainability - You can easily remove an event listener by using the
removeEventListener()
method.
The general syntax for this is:
The first parameter is the type of the event (like “click” or “mousedown” or any other HTML DOM Event.) Here is a link for a complete list of. HTML DOM Events: https://www.w3schools.com/jsref/dom_obj_event.asp
The second parameter is the handler function we call when the event occurs.
The third parameter is a boolean value specifying whether to use event bubbling or event capturing. This parameter is optional.
Consider the code below:
When the button is clicked by the user, we’ve added an event listener for it, and it executes an alert.
We could also write the function definition outside of the addEventListener()
:
In my opinion, the second one is more preferable.
Add many event handlers to the same element
Something like:
is allowed, as well as this:
The addEventListener()
method allows you to add event listeners on any HTML DOM object such as HTML elements, the HTML document, the window object, or other objects that support events, like the xmlHttpRequest object.
Consider this code:
This prints a number to the document, and it continually changes into something random every time the window is resized.
Parsing Parameters
Honestly, not much to do for parsing, straightforward.
Event Bubbling vs Event Capturing
There are two ways of event propagation in the HTML DOM, bubbling and capturing. Event propagation is a way of defining the order of event response. If you have a <p>
element inside a <div>
element, and the user clicks on the <p>
element, which element’s “click” event should be handled first?
In bubbling the inner most element’s event is handled first and then the outer: the <p>
element’s click event is handled first, then the <div>
element’s click event.
In capturing the outer most element’s event is handled first and then the inner: the <div>
element’s click event will be handled first, then the <p>
element’s click event.
With addEventListner()
, we can specify the propagation type, by using the parameter useCapture
. The default value of this is set to false, which will use the bubbling propagation. When the value is set to true, the event used the capturing propagation.
The removeEventListener()
method
The removeEventListener()
method removes event handlers that have been attached with the addEventListener()
method:
JavaScript HTML DOM Navigation
With the HTML DOM, you can navigate the node tree using node relationships
DOM NODES
Again, picture the DOM tree, where each DOM element can be considered a node in a tree. Everything in an HTML document is a node:
- The entire document is a document node
- Every HTML element is an element node
- The text inside HTML elements are text nodes
- Every HTML attribute is an attribute node (deprecated)
- All comments are comment nodes
The relationships between the nodes (parent, child, sibling) are straightforward. Here are some node properties to navigate between nodes in JavaScript:
parentNode
childNodes[nodenumber]
firstChild
lastChild
nextSibling
previousSibling
Child Nodes and Node Values
A common error in DOM processing is to expect an element node to contain text
For example, consider this:
The title element/node, does NOT contain text. It contains a text node with the value “DOM tutorial”. The value of the text node can be accessed by the node’s innerHTML
property:
Accessing the innerHTML property is the same as accessing the nodeValue
of the first child.
Also, as we have an array of child nodes, we can access it this way:
DOM Root Nodes
There are 2 special properties that allow access to the full document:
document.body
- The body of the elementdocument.documentElement
- The full document
The nodeName Property
The nodeName
property specifies the name of a node:
nodeName
is read-onlynodeName
of an element node is the same as the tag namenodeName
of an attribute node is the attribute namenodeName
of a text node is alwaystextnodeName
of a document node is alwaysdocument
Consider the following example:
⇒ Note: the node name always contains the uppercase tag name of an HTML element, if its something like div
, then the nodeName
will be DIV
, in all capitals.
The nodeValue
property
This specifies the value of a node:
nodeValue
for element nodes isnull
nodeValue
for text nodes is the text itselfnodeValue
for attribute nodes is the attribute value (lol)
The nodeType
property
Like above, the nodeType
property is read only. It returns the type of a node. Consider the following:
While you may expect that the above would have the node type of a the
header to have type “h1”, it does not. It has a type of
. Different elements have different integer node types:
These don’t seem particularly useful right now, but in the future they will be.