How Can I Define Events In JavaScript With Examples

admin_img Posted By Bajarangi soft , Posted On 21-09-2020

In html, there are various events which represents that some activity is performed by the user or by the browser. The change in the state of an object is known as an Event. When java script code is included in HTML, js react over these events and allow the execution. This process of reacting over the events is called Event Handling. When the user clicks a button, that click too is an event. Other examples include events like pressing any key, closing a window, resizing a window, etc.

How Can I Define Events In JavaScript With Examples

HTML events are "things" that happen to HTML elements.

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.

Here are some examples of HTML events:

  • An HTML web page has finished loading

  • An HTML input field was changed

  • An HTML button was clicked

Often, when events happen, you may want to do something.

JavaScript lets you execute code when events are detected.

HTML allows event handler attributes, with JavaScript code, to be added to HTML elements.

With single quotes:

<element event='some JavaScript'>

With double quotes:

<element event="some JavaScript">

In the following example, an onclick attribute (with code), is added to a <button> element:

Example(1)

<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(2)

<button onclick="this.innerHTML = Date()">The time is?</button>


Example(3)

<button onclick="displayDate()">The time is?</button>


 

Common HTML Events

Here is a list of some common HTML events:

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



Complete code of JavaScript Events

<!DOCTYPE html>
<html>
<head>
    <title>JavaScript Events</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<style>
    h3{
        color:red;
    }
</style>
<body>
<div class="container">
    <br>
    <br>
    <div class="text-center">
        <h1>JavaScript Events</h1>
    </div>
    <br>
        <div class="well">
            <button class="btn btn-primary"   onclick="document.getElementById('demo').innerHTML=Date()">The time is?</button>

            <h1 id="demo"></h1>
        </div>
    <br>
</div>
</body>
</html>

Related Post