2080 6

BIT251 ยท 2080 ยท 5 marks

What are HTML form events? Write HTML script that prints some texts during onchange and onfocus event of some form elements.

What are HTML form events? Write HTML script that prints some texts during onchange and onfocus event of some form elements. [5]

Web Technology IChapter 4: HTML Forms and Input ElementsHTML form events onchange onfocus

Quick answer

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.

HTML Form Events

Definition


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.

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.