4 Html Forms And Input Elements

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.

HTML Forms and Input Elements: notes

Form elements and attributes

The <form> tag wraps all the controls. Its main attributes are given below.

  • action: the URL that will receive the data.
  • method: GET or POST.
  • name and id: identify the form.
  • enctype: how data is encoded. Use multipart/form-data for file uploads.
  • target: where the response is shown.

Common controls are <input>, <label>, <select>, <textarea>, <button>, <fieldset> and <legend>. The <label> tag names a control. Its for attribute matches the control id, so clicking the label focuses the control.

<form action="/register" method="post">
  <fieldset>
    <legend>Student</legend>
    <label for="name">Name</label>
    <input type="text" id="name" name="name">
    <label for="msg">Message</label>
    <textarea id="msg" name="msg" rows="3" cols="30"></textarea>
  </fieldset>
</form>

Text input password and email fields

The <input> tag changes its behaviour with the type attribute.

TypeUse
textSingle line text
passwordHides the typed characters
emailChecks for a valid email format
numberNumbers with min and max
telTelephone number
dateDate picker

Useful attributes are name, value, placeholder, maxlength, required and readonly.

<input type="text" name="user" placeholder="Username" maxlength="20" required>
<input type="password" name="pass" minlength="8" required>
<input type="email" name="mail" placeholder="[email protected]" required>

A password field only hides the characters on screen. It does not encrypt them, so use HTTPS to protect the data in transit.

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 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

EventDescription
onchangeFires when the value of an element is changed and the element loses focus
onfocusFires when an element receives focus (user clicks or tabs into it)
onblurFires when an element loses focus
onsubmitFires when a form is submitted
onresetFires when a form is reset
oninputFires 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, onfocus fires and prints: "Name field is now focused! Please enter your name."

  • When the user changes the Email field and moves away, onchange fires and prints: "Email has been changed to: [email protected]"

  • When the user selects a course from the dropdown, onchange fires and prints: "You selected: CSIT"


Note: onchange fires after the value is changed and the element loses focus, while onfocus fires immediately when the element gains focus.

Open this answer on its own page →