Reading- Introduction to addEventListener
Reading- Introduction to addEventListener
element.addEventListener(eventType, handlerFunction)
element: Refers to the HTML element to which you want to attach the event listener.
event type: Specifies the type of event to listen for (for example: 'click', 'change', 'mouseover', and so on).
handlerFunction: JSON stores configuration settings, application states, and structured data in databases or files due to its simplicity and ease of use.
In comparing event handling methods, you examine scenarios with and without the use of addEventListener.
1. Without addEventListener
In the absence of addEventListener, the HTML embeds an onclick attribute within the button element, triggering the specified function (handleButtonClick()) upon
clicking.
2. With addEventListener
Add an event listener for the 'click' event to the button using addEventListener.
Assign the handleButtonClick function as the event handler to execute when the user clicks the button.
This configuration replicates the previous functionality but employs addEventListener instead of the inline onclick attribute to bind the event handler.
Readability and maintainability: Decoupling JavaScript from HTML enhances code comprehension and maintenance.
Scalability: As your codebase expands, managing event listeners becomes more straightforward. addEventListener enables easy addition or modification of listeners
without HTML changes.
Code reusability: Assigning named functions (for example: handleButtonClick) as event handlers fosters reusable code applicable across various elements or events.
Consistency and best practices: Leveraging addEventListener aligns with modern JavaScript practices, promoting clean code separation and adhering to unobtrusive
JavaScript principles.
Multiple event handlers: A single element can host multiple event handlers for the same event, providing flexibility in managing diverse functionalities triggered by
one event.
Events
JavaScript events represent user-initiated actions in a web browser, such as mouse and keyboard actions and form or window events, enabling dynamic and interactive
web experiences.
1. Mouse events
Mouse events in JavaScript pertain to interactions with the mouse pointer in a web document, including clicks, movements over elements, entering/exiting areas, and
dragging elements. Examples include 'click' (mouse button press and release), 'mouseover' (mouse enters an element), 'mouseout' (mouse leaves an element), and
'mousemove' (mouse moves within an element).
about:blank 1/4
12/28/24, 10:25 AM about:blank
1.1 Click event
The click event triggers when a mouse button is pressed and released on an element, indicating a user interaction. In the example, a button with the ID 'clickButton' has a
click event listener attached. When clicked, an alert with 'Button clicked!' appears.
Code explanation
This code creates an HTML button and uses JavaScript to add a click event listener. When the user clicks the button, it activates an alert that displays the message 'Button
clicked!'
1.2 Mouseover
Mouseover occurs when the mouse enters an element; mouseout happens when it leaves.
Code explanation
This code creates an HTML <div> element (moveArea) with a light coral background. It attaches a 'mousemove' event listener to moveArea, logging the mouse pointer
coordinates to the console on movement.
2. Keyboard events
JavaScript's keyboard events, involving key presses, releases, or holds, encompass 'keydown' (press), 'keyup' (release), and 'keypress' (press and hold). These events
capture keyboard input, enabling actions based on specific key presses.
Code explanation
The code includes creating an HTML <input> field named keyInput and adding event listeners for both 'keydown' and 'keyup' events to this input field. Whenever a user
presses a key within the input field, the console logs the message 'Key pressed down!'. Similarly, upon releasing the key, the console logs 'Key released!'.
2.2 Keypress
The keypress event in JavaScript occurs when a key on the keyboard is pressed down and produces a character value. It specifically represents pressing a key that results
in a character input.
Code explanation
This code created another <input> field (pressInput) in HTML and added an event listener for 'keypress' to pressInput. On pressing a key in the input field, the console
logs 'Key pressed!'
3. Submit events
Form events in JavaScript are specific events associated with HTML forms and their elements. These events enable developers to capture and respond to user interactions
or changes within form elements.
// form id="myForm">
<input type="text" id="textInput">
<input type="submit" value="Submit">
about:blank 2/4
12/28/24, 10:25 AM about:blank
</form>
<script>
document.getElementById('myForm').addEventListener('submit', function(event) {
event.preventDefault(); // Prevents the default form submission behavior
console.log('Form submitted!');
});
</script>
Code explanation
The code establishes an HTML form with a text input and a submit button. An event listener on the form captures the 'submit' event. Upon submission, the listener triggers
a function preventing default behavior and logging 'Form submitted!' to the console.
The code creates a text input in HTML with placeholder text and adds 'Focus' and 'blur' event listeners to the input. When the input gains focus, the 'focus' listener logs
'Input focused', and when it loses focus, the 'blur' listener logs 'Input blurred' to the console.
4. Window events
Window events in JavaScript handle browser windows or document changes, triggering actions for developers to capture and respond to specific states or behaviors.
// <script>
<script>
window.addEventListener('load', function() {
console.log('Page and all resources loaded');
});
</script>
Code explanation
This event adds an event listener to the window object for the 'load' event. When the document and its resources finish loading, the function logs 'Page and all resources
loaded' to the console.
// <script>
window.addEventListener('resize', function() {
console.log('Window resized');
});
</script>
Code explanation
This code adds an event listener to the window object, listening for the 'resize' event. When the browser window resizes, triggers a function logging 'Window resized' to
the console.
Code eplanation
This code creates a <div> in HTML for scrolling and adds an event listener to the window, listening for the 'scroll' event. When the user scrolls the document view, the
'scroll' event triggers a function that logs 'Document scrolled' to the console.
In conclusion, addEventListener is a vital JavaScript method for managing web development events improving code readability and scalability.
about:blank 3/4
12/28/24, 10:25 AM about:blank
about:blank 4/4