Introduction
In this article, we will learn about events in JavaScript. Event simply refers to doing a specific task in the browser. This is what happens when the user clicks a button. There are many different types of events in JavaScript.
Events
Events are actions that can be detected by JavaScript. By using JavaScript, we have the ability to create dynamic interfaces of web pages. Events are used in a combination of functions.
Let’s see the events in JavaScript
-
Onsubmit Event
-
Onreset Event
-
Onkeypress Event
-
Onkeyup Event
-
Ondblclick Event
-
Onunload Event
Onsubmit Event
To perform an onsubmit event, the onsubmit event occurs when the user clicks the submit button. This event is often used to click the Submit button to check forms. This event supports all the browsers.
Syntax
In HTML Syntax,
- <element onsubmit = “Some code”>
In JavaScript Syntax,
- object.onsubmit = “Some code”
Example
This example demonstrates a JavaScript onsubmit event,
- <html>
- <head>
- <meta charset="utf-8">
- <title>Onsubmit Event</title>
- </head>
- <body>
- <h2>Onsubmit Event in JavaScript</h2>
- <h3>Click the button alert msg will come </h3>
- <script type = "text/javascript">
- function sayHello() {
- alert("Hello World")
- }
- </script>
- <form>
- <input type ="button" onclick="sayHello()" value="Say Hello">
- </form>
- </body>
- </html>
Output
Here is the output of the onsubmit event example above.
Onreset Event
The onreset event is when the user form resets data fields in HTML form. Only the <form> tag is supported in this event.
Syntax
In HTML Syntax,
- <element onreset = “Some code”>
In JavaScript Syntax,
- object.onreset = “Some code”
Example
This example demonstrates JavaScript onreset event.
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>Onreset Event</title>
- </head>
- <body>
- <h2>Onreset Event in JavaScript</h2>
- <form onsubmit="submitform" onreset="resetform">
- First name:
- <input type="text" placeholder="Enter your name">
- <br>
- Last name:
- <input type="text" placeholder="Enter your Last name">
- <br>
- Department:
- <input type="text" placeholder="Example:CSE">
- <br>
- <input type="submit" value="submit">
- <input type="reset" value="Reset_fields">
- </form>
- <script type="text/javascript">
- function submitform() {
- alert("Your data was saved successfully");
- }
- function resetform() {
- alert("Are you want to reset the data fields");
- }
- </script>
- </body>
- </html>
Output
Here is the output of the onreset event example above:
Onkeypress Event
A keypress event occurs when you press it. When you release the key, the key location returns the actual key code value of the character.
Syntax
In HTML Syntax,
- <element onkeypress = “Some code”>
In JavaScript Syntax,
- object.onkeypress = “Some code”
Example
This example demonstrates the JavaScript onkeypress event:
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>Onkeypress event</title>
- </head>
- <body>
- <h2>Onkeypress event in Javascript </h2>
- <p>You type in textbox maximun number of letters</p>
- <textarea id="txtmsg" onkeypress="return fcount()"></textarea><br>
- <div id="dvicounter">50</div>
- <script type="text/javascript">
- function fcount() {
- var dvicounter=document.getElementById("dvicounter");
- var count= parseInt(dvicounter.innerText);
- if (count> 0)
- {
- var textmsg=document.getElementById("txtmsg");
- dvicounter.innerText =count-1;
- }
- else
- {
- return false;
- }
- }
- </script>
- </body>
- </html>
Output
Here is an output of the onkeypress event example above:
Onkeyup Event
The OnKeyup event attribute works when the user releases the keyboard key.
Syntax
In HTML Syntax,
- <element onkeyup = “Some code”>
In JavaScript Syntax,
- object.onkeyup = “Some code”
Example
This example demonstrates the JavaScript onkeyup event:
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>Onkeyup Event</title>
- </head>
- <body>
- <h2>onkeyup Event in JavaScript</h2>
- <form name="frm">
- Onkeyup Event:
- <input type="txt" name="n" placeholder="Enter name" onkeyup="demo(this.value)">
- </form>
- <script type="text/javascript">
- function demo(str) {
- document.frm.n.value=str.toUpperCase();
- }
- </script>
- </body>
- </html>
Output
Here is the output of the onkeyup event example above:
Ondblclick Event
The ondblclick event is when the user clicks the button or icons
Syntax
In HTML Syntax,
- <element ondblclick = “Some code”>
In JavaScript Syntax,
- object.ondblclick = “Some code”
Example
This example demonstrates JavaScript ondblclick event:
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>Ondblclick Event</title>
- </head>
- <body>
- <h2>ondblclick Event in JavaScript</h2>
- <button type="submit" ondblclick="doubleclick()">Double_Click</button>
- <script type="text/javascript">
- function doubleclick() {
- alert("This Event is created successfully");
- }
- </script>
- </body>
- </html>
Output
Here is the output of the ondblclick event example above:
Conclusion
In this article we have seen about events in JavaScript. I hope this article is useful to you. Thanks for reading!