Lecture 6-Javascript DOM New
Lecture 6-Javascript DOM New
<html>
<head>
<title>My title</title>
</head>
<body>
<a href=“ftmk.html”>My link</a>
<h1>My header</h1>
</body>
</html>
• According to the W3C HTML DOM standard, 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
• All comments are comment nodes
The HTML DOM: Document Object 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)
• A node can have a number of children
• Siblings (brothers or sisters) are nodes with the same parent
<html> The HTML document can be read as:
• <html> is the root node
• <html> has no parent
<head>
• <html> is the parent of <head> and
<title>DOM Tutorial • <body>
</title> • <head> is the first child of <html>
</head> • <body> is the last child of <html>
and:
• <head> has one child: <title>
<body>
• <title> has one child (a text node):
<h1>DOM Lesson one</h1> "DOM Tutorial"
<p>Hello world!</p> • <body> has two children: <h1> and
</body> <p>
• <h1> has one child: "DOM Lesson one"
• <p> has one child: "Hello world!"
</html>
• <h1> and <p> are siblings
Try It!
Document Objects: Finding HTML Elements by Id
<html>
<body>
<p id="intro">Hello World!</p>
<p>This example demonstrates the
<b>getElementById</b> method!</p>
<script type="text/javascript">
x=document.getElementById("intro");
document.write("<p>The text from the intro
paragraph: " + x.innerHTML + "</p>");
</script>
</body>
</html>
Finding HTML Elements by Tag Name
<html>
<body>
<p>Hello World!</p>
<p>The DOM is very useful!</p>
<p>This example demonstrates the
<b>getElementsByTagName</b> method.</p>
<script type="text/javascript">
x=document.getElementsByTagName("p");
document.write("Text of first paragraph: " +
x[0].innerHTML);
</script>
</body>
</html>
Finding HTML Elements by Class Name
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var x = document.getElementsByClassName("intro");
document.getElementById("demo").innerHTML =
'The first paragraph (index 0) with class="intro": ' +
x[0].innerHTML;
</script>
</body>
</html>
Finding HTML Elements by CSS Selectors
<!DOCTYPE html>
<html>
<body>
<p>Hello World!</p>
<p id="demo"></p>
<script>
var x = document.querySelectorAll("p.intro");
document.getElementById("demo").innerHTML =
'The first paragraph (index 0) with
class="intro": ' + x[0].innerHTML;
</script>
</body>
</html>
Finding HTML Elements by HTML Object Collections
<form id="frm1" action="/action_page.php">
First name: <input type="text" name="fname" value="Donald"><br>
Last name: <input type="text" name="lname" value="Duck"><br><br>
<input type="submit" value="Submit">
</form>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.forms["frm1"];
var text = "";
var i;
for (i = 0; i < x.length ;i++)
{
text += x.elements[i].value +
"<br>";
}
document.getElementById("demo")
DOM HTML
Write Text to the HTML Output Stream
<html>
<body>
<script type="text/javascript">
document.write("Hello World!");
</script>
</body>
</html> Try It!
Write Text with Formatting to the Output
<html>
<body>
<script type="text/javascript">
document.write("<h1>Hello World!</h1>");
</script>
</body>
</html>
Changing HTML Content: The innerHTML Property
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript can Change HTML</h2>
<p id="p1">Hello World!</p> Try It!
<script>
document.getElementById("p1").innerHTML = "New text!";
</script>
<p>The paragraph above was changed by a script.</p>
</body>
</html>
• The easiest way to get the content of an element is by using the innerHTML property.
• The innerHTML property is useful for getting or replacing the content of HTML
elements.
• The innerHTML property can be used to get or change any HTML element, including
<html> and <body>.
JavaScript HTML DOM - Changing CSS
<script>
function displayDate() {
document.getElementById("demo").innerHTML = Date();
}
</script>
<p id="demo"></p>
</body>
</html>
Assign Events Using the HTML DOM
The HTML DOM allows you to assign events to HTML
elements
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
document.getElementById("myBtn").onclick = displayDate;
function displayDate()
{ document.getElementById("demo").innerHTML =
Date();
}
</script>
</body>
DOM Event Listener
JavaScript HTML DOM EventListener
• The addEventListener() method attaches an event handler to
the specified element.
• The addEventListener() method attaches an event handler to
an element without overwriting existing event handlers.
• You can add many event handlers to one element.
• You can add many event handlers of the same type to one element, i.e
two "click" events.
• The addEventListener() method makes it easier to control how
the event reacts to bubbling.
• You can add event listeners to any DOM object not only HTML
elements. i.e the window object.
• When using the addEventListener() method, the
JavaScript is
separated from the HTML markup, for better readability and allows you
to add event listeners even when you do not control the HTML markup.
• You can easily remove an event listener by using the
removeEventListener() method.
Syntax
element.addEventListener(event,
function, useCapture);
The first parameter is the type of the event (like
"click" or "mousedown").
The second parameter is the function we want to
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.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript addEventListener()</h2>
<p id="demo"></p>
<script>
document.getElementById("myBtn").addEventListener("click",
displayDate);
function displayDate()
{ document.getElementById("demo").innerHTML =
Date();
}
</script>
</body>
</html>
30