The onreset attribute is an important component in HTML forms that developers can use to manage user interactions effectively. This article will cover the definition, browser support, syntax, and practical examples of the onreset attribute, making it easier for beginners to understand its application.
Definition
What is the onreset attribute?
The onreset attribute is an event handler that executes a specified JavaScript code when a user resets a form. This event allows developers to implement custom behavior right after a form is reset, which can be useful for providing feedback, clearing data, or even logging information.
Browser Support
Which browsers support the onreset attribute?
Browser | Supported Version |
---|---|
Chrome | All versions |
Firefox | All versions |
Safari | All versions |
Edge | All versions |
Internet Explorer | IE9 and above |
As seen in the table above, the onreset attribute is widely supported across all modern browsers. This makes it a reliable choice for web developers.
Syntax
How to use the onreset attribute in HTML?
The onreset attribute can be included in the <form> tag. Here is the basic syntax:
<form onreset="JavaScript function or code here">
</form>
This syntax allows you to define a JavaScript function or code to be executed when the form is reset.
Example
Example of onreset attribute in a form
Below is a responsive example of how to use the onreset attribute within a form. When the reset button is clicked, a message will be alerted to the user, indicating that the form has been reset.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>onreset Example</title>
<script>
function showResetMessage() {
alert("The form has been reset!");
}
</script>
</head>
<body>
<form onreset="showResetMessage()">
<label for="name">Name:</label>
<input type="text" id="name" name="name"><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email"><br>
<input type="reset" value="Reset">
</form>
</body>
</html>
In this example, when the user fills in the form fields and clicks on the reset button, an alert will pop up displaying the message “The form has been reset!”. This simple demonstration shows how effective the onreset attribute can be in providing immediate feedback to users.
Related Attributes
Other relevant attributes related to onreset
Aside from the onreset attribute, there are several other HTML attributes that are useful when working with forms:
Attribute | Description |
---|---|
onsubmit | Triggered when a form is submitted. |
oninput | Triggered when the user inputs data into a field. |
onchange | Triggered when the value of an input changes. |
onfocus | Triggered when an element gains focus. |
onblur | Triggered when an element loses focus. |
These attributes help enhance user interaction and provide deeper controls over form behavior, ensuring that web applications are more user-friendly.
FAQ
1. What happens if I do not define an onreset attribute?
If you do not define the onreset attribute in your form, the default reset action will occur, and the form fields will simply be cleared without any additional feedback or custom behavior.
2. Can I use multiple JavaScript functions with onreset?
Yes, you can call multiple functions within the onreset attribute by separating them with semicolons. For example: onreset="function1(); function2();"
.
3. Is the onreset attribute supported in mobile browsers?
Yes, the onreset attribute is supported in mobile browsers, so you can use it reliably across various devices.
4. Can I style the reset input button?
Yes, you can style the reset input button using CSS to change its appearance, just like you would with any other HTML element.
Leave a comment