Javascript Dom
Javascript Dom
With the HTML DOM, JavaScript can access and change all the elements of
an HTML document.
With the object model, JavaScript gets all the power it needs to create dynamic
HTML:
In other words: The HTML DOM is a standard for how to get, change, add,
or delete HTML elements.
JavaScript - HTML DOM Methods
HTML DOM methods are actions you can perform (on HTML Elements).
HTML DOM properties are values (of HTML Elements) that you can set or
change.
A property is a value that you can get or set (like changing the content of an
HTML element).
Example
The following example changes the content (the innerHTML) of the <p> element
with id="demo":
Example
<html>
<body>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "Hello World!";
</script>
</body>
</html>
The getElementById Method
The most common way to access an HTML element is to use the id of the
element. In the example above the getElementById method used id="demo" to find
the element.
Method Description
Method Description
Method Description
Later, in HTML DOM Level 3, more objects, collections, and properties were
added.
document.applets Deprecated 1
document.domConfig Obsolete. 3
Example
const element = document.getElementById("intro");
If the element is found, the method will return the element as an object (in
element).
Example
const element = document.getElementsByTagName("p");
This example finds the element with id="main", and then finds all <p> elements
inside "main":
Example
const x = document.getElementById("main");
const y = x.getElementsByTagName("p");
Example
const x = document.getElementsByClassName("intro");
Example
const x = document.querySelectorAll("p.intro");
Example
const x = document.forms["frm1"];
let text = "";
for (let i = 0; i < x.length; i++) {
text += x.elements[i].value + "<br>";
}
document.getElementById("demo").innerHTML = text;
Example explained:
Example
<!DOCTYPE html>
<html>
<body>
<h1 id="id01">Old Heading</h1>
<script>
const element = document.getElementById("id01");
element.innerHTML = "New Heading";
</script>
</body>
</html>
Example explained:
This example changes the value of the src attribute of an <img> element:
Example
<!DOCTYPE html>
<html>
<body>
<img id="myImage" src="smiley.gif">
<script>
document.getElementById("myImage").src = "landscape.jpg";
</script>
</body>
</html>
Example explained:
Date : Tue Apr 11 2023 11:36:53 GMT+0100 (West Africa Standard Time)
Example
<!DOCTYPE html>
<html>
<body>
<script>
document.getElementById("demo").innerHTML = "Date : " + Date(); </script>
</body>
</html>
document.write()
In JavaScript, document.write() can be used to write directly to the HTML
output stream:
Example
<!DOCTYPE html>
<html>
<body>
<p>Bla bla bla</p>
<script>
document.write(Date());
</script>
<p>Bla bla bla</p>
</body>
</html>
JavaScript Forms
JavaScript Form Validation
HTML form validation can be done by JavaScript. If a form field (fname) is
empty, this function alerts a message, and returns false, to prevent the form
from being submitted:
JavaScript Example
function validateForm() {
let x = document.forms["myForm"]["fname"].value;
if (x == "") {
alert("Name must be filled out");
return false;
}
}
Submit
If a form field (fname) is empty, the required attribute prevents this form from
being submitted:
Data Validation
Data validation is the process of ensuring that user input is clean, correct, and
useful.
Server side validation is performed by a web server, after input has been
sent to the server.
Attribute Description
Selector Description
Example
<html>
<body>
<p id="p2">Hello World!</p>
<script>
document.getElementById("p2").style.color = "blue";
</script>
</body>
</html>
Using Events
The HTML DOM allows you to execute code when an event occurs.
Events are generated by the browser when "things happen" to HTML elements:
An element is clicked on
The page has loaded
Input fields are changed
You will learn more about events in the next chapter of this tutorial.
This example changes the style of the HTML element with id="id1", when the
user clicks a button:
Example
<!DOCTYPE html>
<html>
<body>
<h1 id="id1">My Heading 1</h1>
<button type="button"
onclick="document.getElementById('id1').style.color = 'red'">
Click Me!</button>
</body>
</html>