1console.log("hello-world);
console.log("hello-world);
.container {
width: 80%;
}
<pre><code class="language-css">
.container {
width: 80%;
}
</code></pre>
1console.log("hello-world);
console.log("hello-world);
.container {
width: 80%;
}
<pre><code class="language-css">
.container {
width: 80%;
}
</code></pre>
Adding event listeners to elements with a specific class is a common task in web development, allowing you to respond to user interactions with elements that share a common identifier. In this guide, we’ll explore different methods to achieve this using JavaScript.
Let’s start with a simple example to set the context. Consider the following HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Event Listener Demo</title>
<style>
.clickable {
cursor: pointer;
color: blue;
}
</style>
</head>
<body>
<p class="clickable">Element 1</p>
<p class="clickable">Element 2</p>
<p class="clickable">Element 3</p>
<script src="script.js"></script>
</body>
</html>
Now, let’s add an event listener to all elements with the class “clickable” using JavaScript.
// script.js
const clickableElements = document.querySelectorAll('.clickable');
clickableElements.forEach(element => {
element.addEventListener('click', () => {
console.log('Element clicked:', element.textContent);
// Your custom logic goes here
});
});
Alternatively, can use below approach.
// script.js
const clickableElements = document.getElementsByClassName('clickable');
for (const element of clickableElements) {
element.addEventListener('click', () => {
console.log('Element clicked:', element.textContent);
// Your custom logic goes here
});
}
Both approaches achieve the same result, and you can choose the one that fits your needs.
Customize the type of event you want to listen for by replacing ‘click’ with the desired event type. For a list of available event types, refer to the MDN event list here.
If you prefer traditional loops, you can use a for…of
loop as an alternative to forEach()
:
// script.js
const clickableElements = document.querySelectorAll('.clickable');
for (const element of clickableElements) {
element.addEventListener('click', () => {
console.log('Element clicked:', element.textContent);
// Your custom logic goes here
});
}
In the spirit of Test Driven Development ( 😁), lets test our understanding by solving a problem.
Create a button with the class “magic-btn” and add an event listener to log “Abracadabra!” to the console when the button is clicked. You can write code in
script.js
.
Problem (JavaScript)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Magical Button</title>
<style>
.magic-btn {
padding: 10px;
background-color: purple;
color: white;
cursor: pointer;
}
</style>
</head>
<body>
<button class="magic-btn">Cast Spell</button>
<script src="script.js"></script>
</body>
</html>
Please attempt before seeing the Answer:
// script.js
const magicButton = document.querySelector('.magic-btn');
magicButton.addEventListener('click', () => {
console.log('Abracadabra!');
// Can do more Magical Shenanigans here! 🔮
});
Hope you will make all those HTML elements listen to your Javascript. Keep coding and casting those programming spells! ✨🧙♂️💻
Feel free to reach out!