Form
Form Event
HTML form
event attributes are used to handle events related to form submission, input changes, and other interactions within a form. These events allow developers to control form behavior and validate user inputs.
Common Form Event
onsubmit
: Triggered when a form is submitted. It can be used to validate data or prevent the form from submitting.
<form onsubmit="alert('Form submitted!'); return false;">
onreset
: Fired when a form is reset (e.g., when the reset button is clicked).
<form onreset="alert('Form reset!')">
oninput
: Activated when the value of an input element is changed (e.g., typing in a text box).
<input type="text" oninput="alert('Input changed!')">
onchange
: Triggered when the value of an input element changes and loses focus.
<input type="text" onchange="alert('Input value changed!')">
onfocus
: Fired when an input element gains focus (e.g., when the user clicks on a text field).
<input type="text" onfocus="alert('Input focused!')">
onblur
: Occurs when an input element loses focus (e.g., when the user clicks outside the input field).
<input type="text" onblur="alert('Input lost focus!')">
These event attributes help manage form interactions, enabling real-time validation, feedback, and customized form
behavior.
Conclusion
HTML form
event attributes provide developers with powerful tools for managing user interactions within forms. By using events like onsubmit
, onreset
, oninput
, and onchange
, developers can customize form behavior, validate inputs, and provide real-time feedback. These attributes allow for greater control over form interactions, improving both user experience and form functionality. Whether you're validating data before submission or handling focus and blur events for better usability, these form events are essential for creating dynamic, interactive forms on the web.
ondrop
The ondrop event is triggered when a draggable item is dropped onto a target. Drag-and-drop functionality is commonly used in HTML to facilitate interactive content manipulation.
onblur
The onblur event is triggered when an HTML element loses focus. This event is commonly used with input fields.