Js Events and Dom
Js Events and Dom
JavaScript Events
HTML events are "things" that happen to HTML elements.
HTML Events
An HTML event can be something the browser does, or something a user does.
Each available event has an event handler, which is a block of code (usually a
JavaScript function that you as a programmer create) that will be run when the
event fires.
Example
<button onclick="document.getElementById('demo').innerHTML = Date()">The
time is?</button>
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):
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:
Example
<button onclick="displayDate()">The time is?</button>
Event Description
onmouseout The user moves the mouse away from an HTML element
Many different methods can be used to let JavaScript work with events:
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, getElementById is a method, while innerHTML is
a property.
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.
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.
Method Description
Later, in HTML DOM Level 3, more objects, collections, and properties were
added.
Property Description
document.lastModified Returns the date and time the document was updated
document.links Returns all <area> and <a> elements that have a href at
Mouse Over Me
Click Me
Reacting to Events
A JavaScript can be executed when an event occurs, like when a user clicks on
an HTML element.
onclick=JavaScript
In this example, the content of the <h1> element is changed when a user clicks
on it:
Example
<!DOCTYPE html>
<html>
<body>
</body>
</html>
Example
<!DOCTYPE html>
<html>
<body>
<script>
function changeText(id) {
id.innerHTML = "Ooops!";
}
</script>
</body>
</html>
Example
Assign an onclick event to a button element:
Example
Assign an onclick event to a button element:
<script>
document.getElementById("myBtn").onclick = displayDate;
</script>
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.
Example
<body onload="checkCookies()">
Example
<input type="text" id="fname" onchange="upperCase()">
Mouse Over Me