Javascript Events


Published on: July 03, 2021 By T.Andrew Rayan

The events in javascript is used to handle or react to any actions in Html elements. Whenever any event takes place in HTML elements, if we need to take any actions for it, we use javascript events.

Examples of Html events:

*) Button click event

*) Input text change event.

*) Page load event.

Example of button click event:

When clicking on the button we would call a function named printMessage and prints Button Clicked to the console. This is wired by the onclick event.

Example of Input change event:

In the above example when we type some values to the input control and take the cursor out of the input control, it will print the value in the input control.

Example of keydown event:

The onkeydown event is triggered whenever a key is pressed on the keyboard. Here whenever we type some values in the input text control then the value will be printed to the control.

Example for page load event:

In the above example we have used onload event to call the onLoad() function when the html page is loaded.

Example for mouse events:

We have two mouse events for identifying the mouse over and mouser out events. The onmouseover event is used to identify the mouseover event and onmouseout event is used to identify the mouseout event.

In the above example, when we place the mouse on the p element, the color of the element changes to red and when we move away from the element then its color is restored to black color.


Most Read