Mada za sehemu hiiImplementing web scriptingMada 4
- Implementing variables
- Implementing selection, loop, and array
- Implementing basics of functions
- Implementing event handling
Event Handling in Web Pages
When you browse a website, you often click buttons, move your mouse over images, or fill in forms. Each of these actions triggers an event — a signal that tells the web page something has happened. Event handling is the process of writing code that responds to these events, making web pages interactive and dynamic.
An event is an action or occurrence that happens in the browser, usually started by the user. The browser listens for these events and can run specific code when they occur.
Common Events You Will Use
- onclick — triggered when an element is clicked
- onmouseover — triggered when the mouse moves over an element
- onmouseout — triggered when the mouse moves away from an element
- onload — triggered when a page or image finishes loading
- onsubmit — triggered when a form is submitted
An event handler is a piece of code (usually JavaScript) that runs when a specific event occurs. The simplest way for beginners to add an event handler is using inline event attributes directly inside HTML tags.
Basic Syntax
<element eventhandler="JavaScript code">content</element>
For example, to show a message when a button is clicked:
<button onclick="alert('Habari!')">Click Me</button>
When the user clicks the button, the browser runs the alert() function and displays a pop-up message.
Let us create a simple interactive page with a button that changes its own text when clicked.
HTML Code
<!DOCTYPE html>
<html>
<head>
<title>Mfano wa Event Handling</title>
</head>
<body>
<h1>Karibu kwenye Tovuti Yetu</h1>
<button onclick="changeText()">Bonyeza hapa</button>
<script>
function changeText() {
document.querySelector('button').innerHTML = "Asante!";
}
</script>
</body>
</html>
How It Works
- The
<button>element has anonclickattribute that calls the functionchangeText(). - The
<script>tag contains thechangeText()function. - When the button is clicked, the function runs and changes the button text from "Bonyeza hapa" to "Asante!".
You can also use onmouseover and onmouseout to change the appearance of an element when the mouse moves over it.
HTML Code
<!DOCTYPE html>
<html>
<head>
<title>Mfano wa Mouse Hover</title>
<style>
#box {
width: 200px;
height: 100px;
background-color: lightblue;
text-align: center;
padding-top: 40px;
}
</style>
</head>
<body>
<div id="box" onmouseover="changeColor()" onmouseout="resetColor()">
Weka pointer hapa
</div>
<script>
function changeColor() {
document.getElementById('box').style.backgroundColor = "orange";
}
function resetColor() {
document.getElementById('box').style.backgroundColor = "lightblue";
}
</script>
</body>
</html>
How It Works
- The
<div>element has two event attributes:onmouseoverandonmouseout. - When the mouse enters the box,
changeColor()runs and turns the background orange. - When the mouse leaves the box,
resetColor()runs and returns the background to lightblue.
- Events are actions like clicking, hovering, or loading that happen in the browser.
- Event handlers are JavaScript functions that run in response to events.
- Use inline attributes like
onclick,onmouseover, andonmouseoutto attach handlers to HTML elements. - The
document.querySelector()anddocument.getElementById()methods are used inside functions to change HTML elements. - Always write your JavaScript inside
<script>tags, either in the<head>or at the end of the<body>.
In Tanzania, small businesses such as duka la vitu vya mkono (craft shops) in Arusha or Dar es Salaam can use event handling to create simple online order forms. For example, a customer clicking a "Ongeza kwenye cart" button triggers an event that updates the total price in Tanzania shillings, making the shopping experience interactive and helping the shop owner manage orders more easily.
Jinsi utakavyotathminiwaTathmini ya mchakato · Tathmini ya matokeo · Tathmini ya maarifa
Tathmini ya mchakato
- Attach event handlers to HTML elements.
- Use inline and external event handling methods.
- Test event-driven scripts for interactivity.
Tathmini ya matokeo
Web pages responding correctly to user actions are implemented according to requirements.
Tathmini ya maarifa
Detailed knowledge of:
Method used: The student should explain how to:
- handle events in JavaScript.
Principles: The student should explain the principle of:
- event propagation model (bubbling and capturing).
Theories: The student should explain:
- Inline vs. external event handling.
Circumstantial knowledge:
Detailed knowledge about:
- common events in web programming
Swali
What is an event in web programming?
Ingia ili kuwasilisha jibu lako na lihesabiwe katika umahiri wako.
Ingia ili kufanya mazoeziMwalimu
Umekwama? Niulize chochote kuhusu mada hii.
Ingia ili kumuuliza Mwalimu wa AI wa Sonza kuhusu swali hili.
Ingia ili kuulizaMajadiliano
Hakuna maswali bado