Open In App

Web API UI Events

Web API UI Events is a system that is used to handle the event that occurs in the UI layer. This UI event can be a mouse event or keyboard event. This API provides some use full modules to handle events. These events helped us to design the dynamic UI for the web pages and web applications. There are two ways events can occur one is by Element of Dom and another is by window of web browser. When an event is triggered an ‘inputEvent’ is passed to the event handler which contains.

Interfaces:

Events:

Example 1: In this example we will see different UI event such as onload, keydown, blur, dobleclick and auxclick and print them when they triggered.






<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
 
<body>
    <center>
        <h2 style="color: green">Geeksforgeeks</h2>
        <input type="text" id="ele">
        <br />
        <button id="button" onclick="document
        .getElementById('ele').value=''">clear</button>
        <h2 id="pr"></h2>
    </center>
    <script>
        const doc = document.getElementById('ele');
        window.onload = (event) => {
            print("page is fully loaded");
        };
        doc.addEventListener('keydown', (event) =>
            print('Entered Character is : ' + event.code));
        doc.addEventListener('blur', (event) =>
            print('Input is blured'));
 
        const button = document.getElementById('button');
        button.addEventListener('dblclick', (event) =>
            print('Clear button is doubled clicked'))
        button.addEventListener('auxclick', (event) =>
            print('Clear button is doubled11 clicked'))
        function print(temp) {
            document.getElementById('pr').innerHTML = temp;
        }
    </script>
</body>
 
</html>

Output:

event

Example 2: In this example we will see UI event such as click, mouserover, mouseout event and print them in DOM






<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
 
<body>
    <center>
        <h2 id="ele" style="color: green;">
              Geeksforgeeks
          </h2>
        <button id="button">click</button>
        <h2 id="pr"></h2>
    </center>
    <script>
        const ele = document.querySelector("#ele");
        const button = document.querySelector('button')
        button.addEventListener('click', (event) =>
            print('Button Clicked'));
        button.addEventListener('mouseover', (event) =>
            print('Mouse On button'));
 
        ele.addEventListener('mouseover', (event) =>
            print('Mouse is over GeekforGeeks'))
 
        ele.addEventListener("mouseout", (event) => {
            print("Mouse is out from Geeksforgeeks")
        });
 
 
        function print(ele) {
            document.getElementById('pr').innerHTML = ele;
        }   
    </script>
</body>
 
</html>

Output:

event2


Article Tags :