Table of Contents
A JavaScript can be executed when an event occurs, like when a user clicks on an HTML element.
To execute code when a user clicks on an element, add JavaScript code to an HTML event attribute:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript HTML DOM Events</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Reacting to Events</h4>
<button onclick="document.getElementById('demo').innerHTML=Date()">The time is?</button>
<p id="demo"></p>
</body>
</html>

Result View Example
To assign events to HTML elements you can use event attributes like onclick, onmouseover, etc.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript HTML DOM Events</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>HTML Event Attributes</h4>
<p>Click the button to display a message.</p>
<button onclick="displayDate()">Click me</button>
<p id="demo"></p>
<script>
function displayDate() {
document.getElementById("demo").innerHTML = Date();
}
</script>
</body>
</html>

Result View Example
The HTML DOM allows you to assign events to HTML elements using JavaScript:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript HTML DOM Events</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Assign Events Using the HTML DOM</h4>
<button id="myBtn">Click me</button>
<p id="demo"></p>
<script>
document.getElementById("myBtn").onclick = function() {
document.getElementById("demo").innerHTML = "Button clicked!";
};
</script>
</body>
</html>

Result View Example
The onload event is often used to check the visitor's browser type and cookie version.
The onload event can be used to deal with cookies:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript HTML DOM Events</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>The onload Event</h4>
<p id="demo"></p>
<script>
window.onload = function() {
document.getElementById("demo").innerHTML = "Page fully loaded!";
};
</script>
</body>
</html>

Result View Example
The oninput event fires when a user writes something in an input field:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript HTML DOM Events</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>The oninput Event</h4>
<p>Write something in the input field:</p>
<input type="text" id="myInput" oninput="myFunction()">
<p id="demo"></p>
<script>
function myFunction() {
let x = document.getElementById("myInput").value;
document.getElementById("demo").innerHTML = "You wrote: " + x;
}
</script>
</body>
</html>

Result View Example
The onchange event fires when a user changes the value of an input element:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript HTML DOM Events</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>The onchange Event</h4>
<p>Select a car:</p>
<select id="mySelect" onchange="myFunction()">
<option value="Volvo">Volvo</option>
<option value="BMW">BMW</option>
<option value="Audi">Audi</option>
</select>
<p id="demo"></p>
<script>
function myFunction() {
let x = document.getElementById("mySelect").value;
document.getElementById("demo").innerHTML = "You selected: " + x;
}
</script>
</body>
</html>

Result View Example
Document in project
You can Download PDF file.