Web Technology I · Unit 4
HTML Forms and Input Elements
Exam-focused notes for HTML Forms and Input Elements (Web Technology I, BIT251): what the TU syllabus asks and how it has actually been tested, with 1 solved past questions from this unit.
HTML Forms and Input Elements in short
Forms let a user send data to a server. Login pages, sign up pages and search boxes all use forms. This chapter explains the form tags, the input types and how a form is sent.
What this unit covers
- Form elements and attributes
- Text input password and email fields
- Dropdown select elements
- Radio buttons and checkboxes
- Form submission and structure
- HTML form events onchange onfocus
HTML form events onchange onfocus
2080 · 5 marks
What are HTML form events? Write HTML script that prints some texts during onchange and onfocus event of some form elements. [5]
Show solved answerHide answer
HTML Form Events
Definition
HTML form events are events that are triggered by user interactions with HTML form elements such as input fields, text areas, checkboxes, dropdown menus, and buttons. These events allow JavaScript to respond dynamically when a user interacts with form elements.
Common HTML Form Events
| Event | Description |
|---|---|
onchange | Fires when the value of an element is changed and the element loses focus |
onfocus | Fires when an element receives focus (user clicks or tabs into it) |
onblur | Fires when an element loses focus |
onsubmit | Fires when a form is submitted |
onreset | Fires when a form is reset |
oninput | Fires when the value of an element changes (immediately) |
HTML Script Example
<!DOCTYPE html>
<html>
<head>
<title>HTML Form Events</title>
</head>
<body>
<h2>HTML Form Events Demo</h2>
<form>
<!-- onfocus event on text input -->
<label>Name:</label>
<input
type="text"
id="nameField"
onfocus="document.getElementById('msg1').innerHTML =
'Name field is now focused! Please enter your name.'"
placeholder="Enter your name"
/>
<!-- onchange event on text input -->
<label>Email:</label>
<input
type="text"
id="emailField"
onchange="document.getElementById('msg2').innerHTML =
'Email has been changed to: ' + this.value"
onfocus="document.getElementById('msg1').innerHTML =
'Email field is now focused! Please enter your email.'"
placeholder="Enter your email"
/>
<!-- onchange event on dropdown -->
<label>Select Course:</label>
<select
id="courseSelect"
onchange="document.getElementById('msg3').innerHTML =
'You selected: ' + this.value"
onfocus="document.getElementById('msg1').innerHTML =
'Dropdown is focused! Please select a course.'"
>
<option value="">-- Choose --</option>
<option value="CSIT">BSc CSIT</option>
<option value="BCA">BCA</option>
<option value="BIT">BIT</option>
</select>
</form>
<!-- Output messages -->
<p id="msg1" style="color: blue;"></p>
<p id="msg2" style="color: green;"></p>
<p id="msg3" style="color: red;"></p>
</body>
</html>
Output Explanation
-
When the user clicks on the Name field,
onfocusfires and prints:"Name field is now focused! Please enter your name." -
When the user changes the Email field and moves away,
onchangefires and prints:"Email has been changed to: [email protected]" -
When the user selects a course from the dropdown,
onchangefires and prints:"You selected: CSIT"
Note:
onchangefires after the value is changed and the element loses focus, whileonfocusfires immediately when the element gains focus.
Make Unit 4 stick
Practice BIT251 with flashcards & quizzes