JavaScript Events

The change in the state of an object is known as an Event. In html, there are various events which represents that some activity is performed by the user or by the browser. When javascript 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. Thus, js handles the HTML events via Event Handlers.

For example, when a user clicks over the browser, add js code, which will execute the task to be performed on the event.

Some of the HTML events and their event handlers are:

Mouse events:

Event PerformedEvent HandlerDescription
clickonclickWhen mouse click on an element
mouseoveronmouseoverWhen the cursor of the mouse comes over the element
mouseoutonmouseoutWhen the cursor of the mouse leaves an element
mousedownonmousedownWhen the mouse button is pressed over the element
mouseuponmouseupWhen the mouse button is released over the element
mousemoveonmousemoveWhen the mouse movement takes place.

Keyboard events:

Event PerformedEvent HandlerDescription
Keydown & Keyuponkeydown & onkeyupWhen the user press and then release the key

Form events:

Event PerformedEvent HandlerDescription
focusonfocusWhen the user focuses on an element
submitonsubmitWhen the user submits the form
bluronblurWhen the focus is away from a form element
changeonchangeWhen the user modifies or changes the value of a form element

Window/Document events

Event PerformedEvent HandlerDescription
loadonloadWhen the browser finishes the loading of the page
unloadonunloadWhen the visitor leaves the current webpage, the browser unloads it
resizeonresizeWhen the visitor resizes the window of the browser

Let’s discuss some examples over events and their handlers.

Click Event

<html>  

<head> Javascript Events </head>  

<body>  

<script language="Javascript" type="text/Javascript">  

    <!--  

    function clickevent()  

    {  

        document.write("This is JavaTpoint");  

    }  

    //-->  

</script>  

<form>  

<input type="button" onclick="clickevent()" value="Who's this?"/>  

</form>  

</body>  

</html>

MouseOver Event

Focus Event

<html>  

<head> Javascript Events</head>  

<body>  

<h2> Enter something here</h2>  

<input type="text" id="input1" onfocus="focusevent()"/>  

<script>  

<!--  

    function focusevent()  

    {  

        document.getElementById("input1").style.background=" aqua";  

    }  

//-->  

</script>  

</body>  

</html>

Keydown Event

<html>  

<head> Javascript Events</head>  

<body>  

<h2> Enter something here</h2>  

<input type="text" id="input1" onkeydown="keydownevent()"/>  

<script>  

<!--  

    function keydownevent()  

    {  

        document.getElementById("input1");  

        alert("Pressed a key");  

    }  

//-->  

</script>  

</body>  

</html>

Load event

<html>  

<head>Javascript Events</head>  

</br>  

<body onload="window.alert('Page successfully loaded');">  

<script>  

<!--  

document.write("The page is loaded successfully");  

//-->  

</script>  

</body>  

</html>


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *