Maharashtra's Most Trusted
Chapter 3 - Form & Event Handling
For MSBTE Diploma CO / IT / AIML Branch
Forms in HTML -:
In HTML, a form is a section that allows users to input data and submit it to a server for processing. Forms are essential for interactive web applications, such as login pages, search engines, and online shopping. The main element to create a form is the <form>
tag, which contains various input controls like text fields, checkboxes, radio buttons, and submit buttons.
Basic Structure of a Form
action
: Specifies the URL where the form data will be sent when the form is submitted.method
: Defines how the data is sent to the server. Common values are:GET
: Sends data via the URL, visible to users.POST
: Sends data in the body of the request, often used for sensitive data.
Common Form Elements
Text Input (<input type="text">
)
Password Input (<input type="password">
)
Radio Buttons (<input type="radio">
)
Checkbox (<input type="checkbox">
)
Select Dropdown (<select>
)
Textarea (<textarea>
)
Submit Button (<input type="submit">
)
Example of a Complete Form
Event Handling in Java Script -:
When an event occurs, an event object is created automatically and passed to the event handler function. This object contains useful information about the event, such as the element that triggered the event, mouse coordinates, key presses, etc.
Form events in JavaScript are events triggered by actions on form elements like input fields, select boxes, checkboxes, and the form itself. These events allow you to capture and respond to user interactions within a form, such as when a user submits the form, changes a field, or focuses on an input element.
Mouse Event
Mouse events in JavaScript are triggered when a user interacts with a web page using a mouse or similar input device. These events are particularly useful in forms when you want to enhance user interactions such as providing feedback when hovering over buttons or inputs, or responding to clicks or double clicks on form elements.
Here are some common mouse events that are useful in forms:
Common Mouse Events
click
: Fired when an element is clicked.dblclick
: Fired when an element is double-clicked.mousedown
: Fired when a mouse button is pressed down on an element.mouseup
: Fired when the mouse button is released.mouseover
: Fired when the mouse pointer is moved onto an element.mouseout
: Fired when the mouse pointer is moved away from an element.mousemove
: Fired continuously as the mouse pointer moves over an element.
Mouse Events in the Context of Forms
Let’s look at how to handle mouse events within a form. These can enhance user interaction with feedback on form elements such as buttons, checkboxes, or input fields.
1. click
Event on a Form Button
The click
event is the most commonly used mouse event in forms, especially for submit buttons or any button-like elements.
Example: Submit Button with Click Event
2. mouseover
and mouseout
Events on Form Elements
The mouseover
and mouseout
events are used to detect when the mouse enters and leaves an element. These are useful for providing visual feedback, like changing styles when a user hovers over a form element.
Example: Changing Button Color on Hover
3. mousedown
and mouseup
Events on Buttons
The mousedown
event fires when the mouse button is pressed down, and mouseup
fires when the mouse button is released. These events can be used to provide feedback when a button is clicked, even before the click
event occurs.
Example: Button Press Feedback
4. mousemove
Event for Real-Time Feedback
The mousemove
event fires continuously as the mouse moves over an element. This can be used to create real-time interactive feedback in forms, such as dynamically tracking mouse position or displaying real-time hover effects.
Example: Tracking Mouse Movement Over a Form
5. dblclick
Event on Form Elements
The dblclick
event is fired when a user double-clicks on an element. This is less commonly used in forms but can be useful in special cases, such as resetting form fields or triggering secondary actions.
Example: Reset Form on Double Click
Mouse Event
keydown
: Triggered when a key is pressed down.keyup
: Triggered when a key is released.keypress
: Triggered when a key that produces a character value is pressed. However, this event is now considered deprecated, and it’s recommended to usekeydown
orkeyup
instead.
Example: Key Event Handling in a Form
Let’s explore how key events work in a form using an example where we:
- Prevent non-numeric input in a field.
- Submit the form when the “Enter” key is pressed.
- Display the key being pressed in real-time.
Example Code:
end of chapter 3 -------->
Explore Other Chapters