Handling Events and User Input

Tutorial 3 of 5

Handling Events and User Input

1. Introduction

In this tutorial, we'll dive into the world of event handling and user interaction within GUI applications. We will be working with JavaScript, a language widely used for client-side scripting in web-development and is also very handy in handling events and user input in GUI applications.

By the end of this tutorial, you'll be able to:
- Understand what event handlers are.
- Respond to user interactions with event handlers.
- Manipulate the Document Object Model (DOM) with JavaScript.

Prerequisites:
This tutorial is suitable for beginners. However, a basic understanding of HTML and JavaScript would be beneficial.

2. Step-by-Step Guide

Event handlers are a way for JavaScript to interact with HTML. They monitor specific events like mouse clicks or key presses. Once the event occurs, the event handler executes the specified JavaScript code.

We can add event handlers in JavaScript in two ways:
- HTML Event Handlers (Not recommended due to separation of concerns)
- DOM Level Event Handlers

For best practices, we'll focus on DOM Level Event Handlers.

DOM Level Event Handlers

These are methods that are called when a certain event occurs. The most common event handler is the onclick event handler.

Here's an example:

document.getElementById("myButton").onclick = function() {
  alert("You clicked the button!");
}; 

3. Code Examples

Example 1: Handling Click Events

<!-- HTML code -->
<button id="myButton">Click me</button>
// JavaScript code
document.getElementById("myButton").onclick = function() {
  alert("You clicked the button!");
}; 

In the above example, once the button is clicked, an alert box with the message "You clicked the button!" will pop up.

Example 2: Handling Mouse Over Events

<!-- HTML code -->
<p id="myText">Hover over me!</p>
// JavaScript code
document.getElementById("myText").onmouseover = function() {
  this.style.color = 'red';
}; 

In this example, the text color will change to red when the mouse is moved over the paragraph text.

4. Summary

In this tutorial, we've learned about handling events and user input in JavaScript. We've understood what event handlers are and how they are used to respond to user interactions.

5. Practice Exercises

  1. Exercise 1: Create a button that changes its own text when clicked.
  2. Exercise 2: Create a paragraph that changes its own color to a random color when the mouse is hovered over it.

Tips for further practice: Try to handle other types of events such as ondblclick, onkeydown, onkeyup, etc.

Solutions:

  1. Solution for Exercise 1:
<!-- HTML code -->
<button id="myButton">Click me</button>
// JavaScript code
document.getElementById("myButton").onclick = function() {
  this.textContent = "You clicked me!";
}; 
  1. Solution for Exercise 2:
<!-- HTML code -->
<p id="myText">Hover over me!</p>
// JavaScript code
document.getElementById("myText").onmouseover = function() {
  this.style.color = 'rgb(' + Math.floor(Math.random() * 256) + ',' + Math.floor(Math.random() * 256) + ',' + Math.floor(Math.random() * 256) + ')';
}; 

In the second solution, a random RGB color is generated when the mouse is hovered over the text.