Event Handlers in JavaScript – Tutorial 22

Hi friends, In previous JavaScript tutorial, we have seen do while loops. Today we are going to learn about event handlers in JavaScript. Event handlers are very important in JavaScript to create more interactive and dynamic web pages. Event handlers basically used to run a bit of code based on user action. For example, pop up a message when the user clicks on a button.

We don’t need to write event handlers in those special script tags. We can use event handlers inline to HTML tags. Let us see one simple example for clear understanding.

Event Handlers in JavaScript
Event Handlers in JavaScript

In this example, we have used onclick event handler. This event handler will pop up a message “Submitted successfully” whenever the user clicks on a button.

Download: Click here to see the output of this program in your browser. Right-click and select Save Link as to download the file.

Note: We can add more than one statement in inline event handlers also. For example, in above example, we have the user only one alert statement with the semicolon, so if we have to add more statement just write them after that semicolon.

Following is the list of all event handlers available in JavaScrit.

Event Handler Used In
onAbort image
onBlur select, text, text area
onChange select, text, textarea
onClick button, checkbox, radio, link, reset, submit, area
onError image
onFocus select, text, testarea
onLoad windows, image
onMouseOver link, area
onMouseOut link, area
onSelect text, textarea
onSubmit form
onUnload window
Also read:  For Loop in JavaScript - Tutorial 19

Just try now with some event handlers and let me know about your outputs. In next JavaScript tutorial, we are going to learn about objects in JavaScript. If you liked this article then share it with your friends. Like our facebook page and subscribe to our newsletter for future updates. If you have any questions, feel free to ask in the comment section below. Have a nice day!

Published by

Mayuresh Joshi

Mayuresh Joshi a web geek and founder of MyClassBook, a popular educational blog since 2012. He has completed his B.Tech (Instrumentation Engg.) from SGGSIE&T, Nanded. He loves to write on electronic projects, SharePoint and other current technologies.

Leave a Reply

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