Javascript HTML Dom Elements
Javascript HTML Dom Elements
Model)
When a web page is loaded, the browser creates a Document Object Model
of the page.
With the object model, JavaScript gets all the power it needs to create
dynamic HTML:
To do so, you have to find the elements first. There are several ways to do
this:
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
const element = document.getElementById("intro");
document.getElementById("demo").innerHTML =
"The text from the intro paragraph is: " + element.innerHTML;
</script>
</body>
</html>
<html>
<body>
<p id="demo"></p>
<script>
const element = document.getElementsByTagName("p");
</script>
</body>
</html>
<html>
<body>
<script>
const x = document.getElementsByClassName("intro");
document.getElementById("demo").innerHTML =
'The first paragraph (index 0) with class="intro" is: ' + x[0].innerHTML;
</script>
</body>
</html>
<html>
<body>
<p id="demo"></p>
<script>
const x = document.querySelectorAll("p.intro");
document.getElementById("demo").innerHTML =
'The first paragraph (index 0) with class="intro" is: ' + x[0].innerHTML;
</script>
</body>
</html>
<html>
<body>
<p id="demo"></p>
<script>
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;
</script>
</body>
</html>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
"Number of anchors are: " + document.anchors.length;
</script>
</body>
</html>
// Body Content
<html>
<body>
<h2>JavaScript HTMLDOM</h2>
<p>Displaying document.body</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = document.body.innerHTML;
</script>
</body>
</html>
//documentElement
<html>
<body>
<h2>JavaScript HTMLDOM</h2>
<p>Displaying document.documentElement</p>
<p id="demo"></p>
</body>
</html>
//Embed
<html>
<body>
<h2>JavaScript HTMLDOM</h2>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
"Number of embeds: " + document.embeds.length;
</script>
</body>
</html>
<form action="">
First name: <input type="text" name="fname" value="Donald">
<input type="submit" value="Submit">
</form>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
"Number of forms: " + document.forms.length;
</script>
//Document Head
<html>
<head>
<title>W3Schools Demo</title>
</head>
<body>
<h2>JavaScript HTMLDOM</h2>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
document.head;
</script>
</body>
</html>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
"Number of images: " + document.images.length;
</script>
<p>
<a href="/html/default.asp">HTML</a>
<br>
<a href="/css/default.asp">CSS</a>
</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
"Number of links: " + document.links.length;
</script>
</body>
</html>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
"Number of scripts: " + document.scripts.length;
</script>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
"The title of this document is: " + document.title;
</script>
</body>
</html>
1
DEEPAK SINGH(ASST. PROFESSOR ,MAHADEV P.G. COLLEGE VARANASI)
0
The following example changes the style of a <p> element:
<html>
<body>
<script>
document.getElementById("p2").style.color = "blue";
document.getElementById("p2").style.fontFamily = "Arial";
document.getElementById("p2").style.fontSize = "larger";
</script>
</body>
</html>
Using Events
The HTML DOM allows you to execute code when an event occurs.
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
<html>
<body>
1
DEEPAK SINGH(ASST. PROFESSOR ,MAHADEV P.G. COLLEGE VARANASI)
1
<button type="button"
onclick="document.getElementById('id1').style.color = 'red'">
Click Me!</button>
</body>
</html>
<div id ="container">
<div id ="animate"></div>
</div>
<script>
function myMove() {
let id = null;
const elem = document.getElementById("animate");
1
DEEPAK SINGH(ASST. PROFESSOR ,MAHADEV P.G. COLLEGE VARANASI)
2
let pos = 0;
clearInterval(id);
id = setInterval(frame, 5);
function frame() {
if (pos == 350) {
clearInterval(id);
} else {
pos++;
elem.style.top = pos + "px";
elem.style.left = pos + "px";
}
}
}
</script>
</body>
</html>
<html>
<body>
<h1>JavaScript HTML Events</h1>
<h2>The onclick Attribute</h2>
</body>
</html>
<html>
<body>
<h1>JavaScript HTML Events</h1>
<h2>The onclick Attribute</h2>
1
DEEPAK SINGH(ASST. PROFESSOR ,MAHADEV P.G. COLLEGE VARANASI)
3
<script>
function changeText(id) {
id.innerHTML = "Ooops!";
}
</script>
</body>
</html>
<html>
<body>
<h1>JavaScript HTML Events</h1>
<h2>The onclick Attribute</h2>
<script>
function displayDate() {
document.getElementById("demo").innerHTML = Date();
}
</script>
<p id="demo"></p>
</body>
</html>
<p id="demo"></p>
1
DEEPAK SINGH(ASST. PROFESSOR ,MAHADEV P.G. COLLEGE VARANASI)
4
<script>
document.getElementById("myBtn").onclick = displayDate;
function displayDate() {
document.getElementById("demo").innerHTML = Date();
}
</script>
</body>
</html>
The onload event can be used to check the visitor's browser type and browser
version, and load the proper version of the web page based on the
information.
The onload and onunload events can be used to deal with cookies.
<html>
<body onload="checkCookies()">
<p id="demo"></p>
<script>
function checkCookies() {
let text = "";
if (navigator.cookieEnabled == true) {
text = "Cookies are enabled.";
} else {
text = "Cookies are not enabled.";
}
document.getElementById("demo").innerHTML = text;
}
</script>
1
DEEPAK SINGH(ASST. PROFESSOR ,MAHADEV P.G. COLLEGE VARANASI)
5
</body>
</html>
<html>
<body>
<h1>JavaScript HTML Events</h1>
<h2>The oninput Attribute</h2>
<script>
function upperCase() {
const x = document.getElementById("fname");
x.value = x.value.toUpperCase();
}
</script>
</body>
</html>
Below is an example of how to use the onchange. The upperCase() function will
be called when a user changes the content of an input field.
<html>
<body>
<h1>JavaScript HTML Events</h1>
<h2>The onchange Attribute</h2>
1
DEEPAK SINGH(ASST. PROFESSOR ,MAHADEV P.G. COLLEGE VARANASI)
6
Enter your name: <input type="text" id="fname" onchange="upperCase()">
<p>When you leave the input field, a function transforms the input to upper
case.</p>
<script>
function upperCase() {
const x = document.getElementById("fname");
x.value = x.value.toUpperCase();
}
</script>
</body>
</html>
<html>
<body>
<h1>JavaScript HTML Events</h1>
<h2>The onmouseover Attribute</h2>
<script>
function mOver(obj) {
obj.innerHTML = "Thank You"
}
function mOut(obj) {
obj.innerHTML = "Mouse Over Me"
}
</script>
1
DEEPAK SINGH(ASST. PROFESSOR ,MAHADEV P.G. COLLEGE VARANASI)
7
</body>
</html>
Thank You
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript HTML Events</h1>
<h2>The onmousedown Attribute</h2>
<script>
function mDown(obj) {
obj.style.backgroundColor = "#1ec5e5";
obj.innerHTML = "Release Me";
}
function mUp(obj) {
obj.style.backgroundColor="#D94A38";
obj.innerHTML="Thank You";
}
</script>
</body>
</html>
1
DEEPAK SINGH(ASST. PROFESSOR ,MAHADEV P.G. COLLEGE VARANASI)
8
JavaScript HTML Events
<p>When the input field gets focus, a function is triggered which changes the
background-color.</p>
</body>
</html>
// Mouse over
<html>
<body>
<h1>JavaScript HTML Events</h1>
<h2>The onmousover Attribute</h2>
<h1 onmouseover="style.color='red'"
onmouseout="style.color='black'">
Mouse over this text</h1>
</body>
</html>
1
DEEPAK SINGH(ASST. PROFESSOR ,MAHADEV P.G. COLLEGE VARANASI)
9
JavaScript HTML DOM
EventListener
<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>
<html>
<body>
<h2>JavaScript addEventListener()</h2>
2
DEEPAK SINGH(ASST. PROFESSOR ,MAHADEV P.G. COLLEGE VARANASI)
0
<p>This example uses the addEventListener() method to add two click events
to the same button.</p>
<script>
var x = document.getElementById("myBtn");
x.addEventListener("click", myFunction);
x.addEventListener("click", someOtherFunction);
function myFunction() {
alert ("Hello World!");
}
function someOtherFunction() {
alert ("This function was also executed!");
}
</script>
</body>
</html>
<html>
<body>
<h2>JavaScript addEventListener()</h2>
<p>Try resizing this browser window to trigger the "resize" event handler.</p>
<p id="demo"></p>
2
DEEPAK SINGH(ASST. PROFESSOR ,MAHADEV P.G. COLLEGE VARANASI)
1
<script>
window.addEventListener("resize", function(){
document.getElementById("demo").innerHTML = Math.random();
});
</script>
</body>
</html>
<html>
<head>
<style>
#myDIV {
background-color: coral;
border: 1px solid;
padding: 50px;
color: white;
font-size: 20px;
}
</style>
</head>
<body>
<h2>JavaScript removeEventListener()</h2>
<div id="myDIV">
<p>This div element has an onmousemove event handler that displays a
random number every time you move your mouse inside this orange field.</p>
<p>Click the button to remove the div's event handler.</p>
<button onclick="removeHandler()" id="myBtn">Remove</button>
</div>
<p id="demo"></p>
2
DEEPAK SINGH(ASST. PROFESSOR ,MAHADEV P.G. COLLEGE VARANASI)
2
<script>
document.getElementById("myDIV").addEventListener("mousemove",
myFunction);
function myFunction() {
document.getElementById("demo").innerHTML = Math.random();
}
function removeHandler() {
document.getElementById("myDIV").removeEventListener("mousemove",
myFunction);
}
</script>
</body>
</html>
<div id="div1">
<p id="p1">This is a paragraph.</p>
<p id="p2">This is another paragraph.</p>
</div>
<script>
const para = document.createElement("p");
const node = document.createTextNode("This is new.");
para.appendChild(node);
const element = document.getElementById("div1");
element.appendChild(para);
</script>
</body>
2
DEEPAK SINGH(ASST. PROFESSOR ,MAHADEV P.G. COLLEGE VARANASI)
3
</html>
If you don't want that you can use the insertBefore() method:
<html>
<body>
<div id="div1">
<p id="p1">This is a paragraph.</p>
<p id="p2">This is another paragraph.</p>
</div>
<script>
const para = document.createElement("p");
const node = document.createTextNode("This is new.");
para.appendChild(node);
</body>
</html>
2
DEEPAK SINGH(ASST. PROFESSOR ,MAHADEV P.G. COLLEGE VARANASI)
4
<html>
<body>
<div>
<p id="p1">This is a paragraph.</p>
<p id="p2">This is another paragraph.</p>
</div>
<script>
function myFunction() {
document.getElementById("p1").remove();
}
</script>
</body>
</html>
<html>
<body>
<div id="div1">
<p id="p1">This is a paragraph.</p>
<p id="p2">This is another paragraph.</p>
</div>
<script>
const parent = document.getElementById("div1");
const child = document.getElementById("p1");
2
DEEPAK SINGH(ASST. PROFESSOR ,MAHADEV P.G. COLLEGE VARANASI)
5
parent.removeChild(child);
</script>
</body>
</html>
<html>
<body>
<div id="div1">
<p id="p1">This is a paragraph.</p>
<p id="p2">This is a paragraph.</p>
</div>
<script>
const parent = document.getElementById("div1");
const child = document.getElementById("p1");
const para = document.createElement("p");
const node = document.createTextNode("This is new.");
para.appendChild(node);
parent.replaceChild(para,child);
</script>
</body>
</html>
2
DEEPAK SINGH(ASST. PROFESSOR ,MAHADEV P.G. COLLEGE VARANASI)
6