JS DOM Events

DOM events are interactions or occurrences that take place within a web page, such as user actions or system-generated events. JavaScript can respond to these events by attaching event listeners to DOM elements, allowing you to execute specific code when the events occur. Event handling is a fundamental aspect of building interactive and dynamic web applications.


Here are some common DOM events and how to use event listeners to respond to them:


1. **click**:

The `click` event occurs when a user clicks on an element.

html
<button id="myButton">Click Me</button>
javascript
const button = document.getElementById('myButton');

button.addEventListener('click', function() {
    console.log('Button clicked!');
});

2. **keydown**:

The `keydown` event occurs when a key on the keyboard is pressed while the element is focused.

html
<input type="text" id="myInput">

javascript
const input = document.getElementById('myInput');

input.addEventListener('keydown', function(event) {
    console.log('Key pressed:', event.key);
});

3. **mouseover** and **mouseout**:

The `mouseover` event occurs when the mouse pointer enters an element, and the `mouseout` event occurs when the mouse pointer leaves the element.

html
<div id="myDiv" style="width: 100px; height: 100px; background-color: red;"></div>

javascript
const div = document.getElementById('myDiv');

div.addEventListener('mouseover', function() {
    div.style.backgroundColor = 'blue';
});

div.addEventListener('mouseout', function() {
    div.style.backgroundColor = 'red';
});

4. **change**:

The `change` event occurs when the value of an input element, like a select or checkbox, changes.

html
<select id="mySelect">
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
</select>
javascript
const select = document.getElementById('mySelect');

select.addEventListener('change', function() {
console.log('Selected value:', select.value);
});

5. **submit**:

The `submit` event occurs when a form is submitted, typically by clicking a submit button.

html
<form id="myForm">
<input type="text" name="username">
<input type="submit" value="Submit">
</form>

javascript
const form = document.getElementById('myForm');

form.addEventListener('submit', function(event) {
event.preventDefault(); // Prevent form submission (for this example)
// Process form data or perform form validation
console.log('Form submitted');
});

These are just a few examples of the many DOM events available. Events allow you to create interactive and responsive web pages by executing JavaScript code in response to user actions or system-generated triggers. Event handling is a powerful tool to build engaging user experiences in web development.



About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext