JavaScript Events BOm and DOm methods (1)
JavaScript Events BOm and DOm methods (1)
When JavaScript is used in HTML pages, JavaScript can "react" on these events.
HTML Events
An HTML event can be something the browser does, or something a user does.
HTML allows event handler attributes, with JavaScript code, to be added to HTML elements.
In the following example, an onclick attribute (with code), is added to a <button> element:
Example
In the example above, the JavaScript code changes the content of the element with id="demo".
In the next example, the code changes the content of its own element (using this.innerHTML):
<!DOCTYPE html>
<html>
<body>
</body>
</html>
Example
<button onclick="this.innerHTML = Date()">The time is?</button>
JavaScript code is often several lines long. It is more common to see event attributes calling
functions:
<!DOCTYPE html>
<html>
<body>
</body>
</html>
Output - Tue Dec 03 2019 11:20:26 GMT-0800 (Pacific Standard Time)
Example
<script>
function displayDate() {
document.getElementById("demo").innerHTML = Date();
}
</script>
<p id="demo"></p>
</body>
</html>
Output
Event Description
onchange An HTML element has been changed
onclick The user clicks an HTML element
onmouseover The user moves the mouse over an HTML element
onmouseout The user moves the mouse away from an HTML element
onkeydown The user pushes a keyboard key
onload The browser has finished loading the page
Event handlers can be used to handle, and verify, user input, user actions, and browser actions:
Many different methods can be used to let JavaScript work with events:
You will learn a lot more about events and event handlers in the HTML DOM chapters.
The Browser Object Model (BOM) allows JavaScript to "talk to" the browser.
Since modern browsers have implemented (almost) the same methods and properties for
JavaScript interactivity, it is often referred to, as methods and properties of the BOM.
All global JavaScript objects, functions, and variables automatically become members of the
window object.
Even the document object (of the HTML DOM) is a property of the window object:
window.document.getElementById("header");
document.getElementById("header");
Window Size
Two properties can be used to determine the size of the browser window.
The browser window (the browser viewport) is NOT including toolbars and scrollbars.
document.documentElement.clientHeight
document.documentElement.clientWidth
or
document.body.clientHeight
document.body.clientWidth
Example
var w = window.innerWidth
|| document.documentElement.clientWidth
|| document.body.clientWidth;
var h = window.innerHeight
|| document.documentElement.clientHeight
|| document.body.clientHeight;
The example displays the browser window's height and width: (NOT including
toolbars/scrollbars)
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var w = window.innerWidth
|| document.documentElement.clientWidth
|| document.body.clientWidth;
var h = window.innerHeight
|| document.documentElement.clientHeight
|| document.body.clientHeight;
var x = document.getElementById("demo");
x.innerHTML = "Browser inner window width: " + w + ", height: " + h + ".";
</script>
</body>
</html>
With the HTML DOM, JavaScript can access and change all the elements of an HTML
document.
The HTML DOM (Document Object 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:
In other words: The HTML DOM is a standard for how to get, change, add, or delete
HTML elements.
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>
In the example above the getElementById method used id="demo" to find the element.
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>.
The HTML DOM document object is the owner of all other objects in your web page.
The HTML DOM Document Object
The document object represents your web page.
If you want to access any element in an HTML page, you always start with accessing the
document object.
Below are some examples of how you can use the document object to access and manipulate
HTML.
Later, in HTML DOM Level 3, more objects, collections, and properties were added.
This page teaches you how to find and access HTML elements in an HTML page.
Finding HTML Elements
Often, with JavaScript, you want to manipulate HTML elements.
To do so, you have to find the elements first. There are several ways to do this:
Example
If the element is found, the method will return the element as an object (in myElement).
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var myElement = document.getElementById("intro");
document.getElementById("demo").innerHTML =
"The text from the intro paragraph is " + myElement.innerHTML;
</script>
</body>
</html>
output
Example
var x = document.getElementsByTagName("p");
This example finds the element with id="main", and then finds all <p> elements inside "main":
Example
var x = document.getElementById("main");
var y = x.getElementsByTagName("p");
----------------------------------------------------------
<!DOCTYPE html>
<html>
<body>
<p>Hello World!</p>
<p>This example demonstrates the <b>getElementsByTagName</b> method.</p>
<p id="demo"></p>
<script>
var x = document.getElementsByTagName("p");
document.getElementById("demo").innerHTML =
'The text in first paragraph (index 0) is: ' + x[0].innerHTML;
</script>
</body>
</html>
output
Example
var x = document.getElementsByClassName("intro");
Finding elements by class name does not work in Internet Explorer 8 and earlier versions.
---------------------------------------------------------
<!DOCTYPE html>
<html>
<body>
<p>Hello World!</p>
<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>
Output –
Finding HTML Elements by Class Name
Hello World!
The DOM is very useful.
This example demonstrates the getElementsByClassName method.
The first paragraph (index 0) with class="intro": The DOM is very useful.
Example
var x = document.querySelectorAll("p.intro");
The querySelectorAll() method does not work in Internet Explorer 8 and earlier versions.
<!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>
Output
Finding HTML Elements by Query Selector
Hello World!
The DOM is very useful.
This example demonstrates the querySelectorAll method.
The first paragraph (index 0) with class="intro": The DOM is very useful.
Example
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").innerHTML = text;
The following HTML objects (and object collections) are also accessible:
document.anchors
document.body
document.documentElement
document.embeds
document.forms
document.head
document.images
document.links
document.scripts
document.title
The HTML DOM allows JavaScript to change the content of HTML elements.
In JavaScript, document.write() can be used to write directly to the HTML output stream:
Example
<!DOCTYPE html>
<html>
<body>
<script>
document.write(Date());
</script>
</body>
</html>
Never use document.write() after the document is loaded. It will overwrite the document.
Changing HTML Content
The easiest way to modify the content of an HTML element is by using the innerHTML property.
Example
<html>
<body>
<script>
document.getElementById("p1").innerHTML = "New text!";
</script>
</body>
</html>
Example explained:
Example
<!DOCTYPE html>
<html>
<body>
<script>
var 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>
<script>
document.getElementById("myImage").src = "landscape.jpg";
</script>
</body>
</html>
Example explained: