HTML Topics
- HTML Intro
- HTML Basic
- HTML Editors
- HTML CSS
- HTML Tags
- HTML Deprecated Tags
- HTML Events
- HTML Event Attributes
- HTML Global Attributes
- HTML Attributes
- abbr
- accept
- accept-charset
- accesskey
- action
- align
- alt
- archive
- as
- async
- autocapitalize
- autocomplete
- autofocus
- autoplay
- axis
- bgcolor
- border
- cellpadding
- cellspacing
- char
- charoff
- charset
- checked
- cite
- class
- classid
- codebase
- codetype
- color
- cols
- colspan
- compact
- content
- contenteditable
- controls
- coords
- crossorigin
- data
- data-*
- datetime
- declare
- default
- defer
- dir
- dirname
- disabled
- download
- draggable
- enctype
- enterkeyhint
- for
- form
- formaction
- formenctype
- formmethod
- formnovalidate
- formtarget
- frame
- frameborder
- frameset
- headers
- height
- hidden
- high
- href
- hreflang
- hspace
- http-equiv
- id
- inert
- inputmode
- ismap
- kind
- label
- lang
- list
- loop
- low
- max
- maxlength
- media
- method
- min
- multiple
- muted
- name
- novalidate
- onabort
- onafterprint
- onbeforeprint
- onbeforeunload
- onblur
- oncanplay
- oncanplaythrough
- onchange
- onclick
- oncontextmenu
- oncopy
- oncuechange
- oncut
- ondblclick
- ondrag
- ondragend
- ondragenter
- ondragleave
- ondragover
- ondragstart
- ondrop
- ondurationchange
- onemptied
- onended
- onerror
- onfocus
- onhashchange
- oninput
- oninvalid
- onkeydown
- onkeypress
- onkeyup
- onload
- onloadeddata
- onloadedmetadata
- onloadstart
- onmousedown
- onmousemove
- onmouseout
- onmouseover
- onmouseup
- onmousewheel
- onoffline
- ononline
- onpagehide
- onpageshow
- onpaste
- onpause
- onplay
- onplaying
- onpopstate
- onprogress
- onratechange
- onreset
- onresize
- onscroll
- onsearch
- onseeked
- onseeking
- onselect
- onstalled
- onstorage
- onsubmit
- onsuspend
- ontimeupdate
- ontoggle
- onunload
- onvolumechange
- onwaiting
- onwheel
- open
- optimum
- pattern
- placeholder
- popover
- popovertarget
- popovertargetaction
- poster
- preload
- readonly
- rel
- required
- reversed
- rows
- rowspan
- sandbox
- scope
- selected
- shape
- size
- sizes
- span
- spellcheck
- src
- srcdoc
- srclang
- srcset
- start
- step
- style
- tabindex
- target
- title
- translate
- type
- usemap
- value
- width
- wrap
- HTML Comments
- HTML Entity
- HTML Head
- HTML Form
- HTML IndexedDB
- HTML Drag & Drop
- HTML Geolocation
- HTML Canvas
- HTML Status Code
- HTML Language Code
- HTML Country Code
- HTML Charset
- MIME Types
HTML onreset Attribute
Photo Credit to CodeToFun
🙋 Introduction
The onreset
attribute is an HTML attribute used with the <form> element to specify a script that will run when a form is reset.
This attribute provides developers with the ability to define custom actions that should be executed when the user clicks the "Reset" button within a form.
🎯 Purpose of onreset
The primary purpose of the onreset
attribute is to allow developers to trigger specific JavaScript functions or actions when a form is reset.
This can be useful for clearing or resetting additional elements, updating dynamic content, or performing other tasks based on user interactions.
💎 Values
The onreset
attribute accepts JavaScript code as its value. This code will be executed when the form is reset.
Developers can assign functions or inline JavaScript statements to this attribute.
📄 Example
Here's a simple example demonstrating the use of the onreset
attribute:
<form onreset="resetForm()">
<label for="name">Name:</label>
<input type="text" id="name" name="name">
<label for="email">Email:</label>
<input type="email" id="email" name="email">
<input type="reset" value="Reset">
</form>
<script>
function resetForm() {
// Custom JavaScript logic to be executed on form reset
console.log("Form has been reset!");
}
</script>
🧠 How it Works
In this example, the onreset
attribute is used to call the resetForm function when the form is reset. The function logs a message to the console, but in a real-world scenario, you might perform more complex operations.
🔄 Dynamic Values with JavaScript
Similar to other HTML attributes, you can dynamically set the onreset
attribute using JavaScript.
This allows you to change the behavior of the form reset dynamically. Here's a brief example:
<script>
// Dynamically set the onreset attribute
document.getElementById("dynamicForm").onreset = function() {
alert("This form is being reset!");
};
</script>
🧠 How it Works
In this script, the onreset
attribute is dynamically set for a form with the id "dynamicForm." The assigned function will display an alert when the form is reset.
🏆 Best Practices
- Use the
onreset
attribute sparingly and only when custom actions are required upon form reset. - Ensure that the JavaScript code assigned to onreset is error-free to prevent issues during execution.
- Keep the user experience in mind and provide clear feedback when custom reset actions are triggered.
🎉 Conclusion
The onreset
attribute in HTML provides a way to enhance form interactions by allowing developers to define specific actions when a form is reset.
By understanding and appropriately utilizing this attribute, you can tailor the reset behavior to meet the specific needs of your web applications.
👨💻 Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (HTML onreset Attribute), please comment here. I will help you immediately.