Lec 8 Basic JavaScript DOM Client Side Validation 03052023 122614pm
Lec 8 Basic JavaScript DOM Client Side Validation 03052023 122614pm
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.
DOM SPECIFICATION
• The HTML DOM can be accessed with JavaScript (and with other
programming languages).
A property is a value that you can get or set (like changing the content
of an HTML element).
A method is an action you can do (like add or deleting an HTML
element).
FINDING HTML ELEMENTS
Often, with JavaScript, you want to manipulate HTML elements. To do so, you
have to find the elements first. There are a couple of ways to do this:
• document.getElementById(id)
• Access a unique element based on id
• document.getElementsByTagName(name)
• Access all elements with specific tag
• document.getElementsByClassName(name)
• Access all elements with specific class applied
FINDING HTML ELEMENTS BY TAG NAME
Returns an array of elements with the given tag name. The example given below finds all <p> elements.
https://www.w3schools.com/jsref/met_document_getelementsbytagname.asp
FINDING HTML ELEMENTS BY CLASS NAME
If you want to find all HTML elements with the same class name, use
getElementsByClassName().
Hello!
How are you?
• [htmlElementObject].[attribute] = [value]
• document.getElementById(“dvTitle”).className = “newClass”
• [htmlElementObject].setAttribute(attribute, value)
• document.getElementById(“dvTitle”).setAttribute(“class”, “newClass”)
• [htmlElementObject].style.[property] = [value]
• document.getElementById(“dvTitle”).style.color = “red”
ADDING AND DELETING ELEMENTS
Important Functions
https://www.w3schools.com/js/js_htmldom_document.asp
JAVASCRIPT HTML DOM EVENTS
With the HTML DOM, you can navigate the node tree using node relationships.
Document Node: the root node representing the entire HTML document.
Element node: represents an HTML element (or tag). An element node may have child nodes,
which can be either element or text node. Element node may also have attributes.
Text Node: contains the text content of an element.
Others: such as comment, attribute.
Node Relationships
The nodes in the node tree
have a hierarchical relationship
to each other. The terms
parent, child, and sibling are
used to describe the
relationships.
•In a node tree, the top node is
called the root (or root node)
•Every node has exactly one
parent, except the root (which
has no parent)
With the HTML DOM, all nodes in the node tree can •A node can have a number of
be accessed by JavaScript. New nodes can be children
created, and all nodes can be modified or deleted. •Siblings (brothers or sisters)
are nodes with the same parent
DOM NODE PROPERTIES
1. nodeName: contain the name of the node, which is 3. nodeValue: contain the value of the node.
read-only.
◦ nodeValue for Text node is the
◦ The nodeName for an Element node is text contained;
the tag-name; ◦ nodeValue for Element node is
◦ nodeName for the Document node undefined.
is #document;
4. parentNode: reference to parent node. There
◦ nodeName for Text nodes is #text. is only one parent node in a tree structure.
You can use the following node properties to navigate between nodes with 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.
The element node <title> (in the example above) does not contain text.
It contains a text node with the value "DOM Tutorial".
CHILD NODES AND NODE
TheVALUES
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:
There are two special properties that allow access to the full document:
document.documentElement - The full document returns the Element that is the root element of the
document.
document.body
◦ The body property sets or returns the
document's body.
• To add a new element to the HTML DOM, you must create the element (element node)
first, and then append it to an existing element.
CREATING NEW HTML ELEMENTS - insertBefore()
• The insertBefore() method inserts a node as a child, right before an existing child,
which you specify.
EXAMPLE
EXAMPLE
REPALCING THE NODE
The replaceChild() method replaces a
child node with a new node.
This example creates a collection of all <p> elements. Then displays the length of the collection.
HTMLCollection may look like an array, but it is not. You can loop through the list and
refer to the elements with a number (just like an array).However, you cannot use
array methods like valueOf(), pop(), push(), or join() on an HTMLCollection.
JavaScript Events
Mouse Events
• onmouseenter
• onmouseleave
• onmouseover
• onmouseout
• onmousemove
• Many more……..
JavaScript Events
Input events
◦ onsubmit - When a user clicks the submit button
◦ onblur - When a user leaves an input field
◦ onchange - When a user changes the content of an input field
◦ onchange - When a user selects a dropdown value
◦ onfocus - When an input field gets focus
◦ onselect - When input text is selected
◦ onreset - When a user clicks the reset button
◦ onkeydown - When a user is pressing/holding down a key
◦ onkeypress - When a user is pressing/holding down a key
◦ onkeyup - When the user releases a key
JavaScript Events (contd.)
Click Events
◦ onclick - When button is clicked
◦ ondblclick - When a text is double-clicked
Load Events
◦ onload - When the page has been loaded
◦ onload - When an image has been loaded
◦ onerror - When an error occurs when loading an image
◦ onunload - When the browser closes the document
◦ onresize - When the browser window is resized
What is BOM?
Browser Object Model, used to talk to browser