Add event listeners
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="eventlistener.css">
<title>Document</title>
</head>
<body>
<h1>This is heading</h1>
<button class="button" id="button">click me 1</button>
<button class="button" id="button">click me 2</button>
<button class="button" id="button">click me 3</button>
<button class="button" id="button">click me 4</button>
<button class="button" id="button">click me 5</button>
<script src="eventlisteners.js"></script>
</body>
</html>
// JAVA SCRIPT
// let btn=document.getElementById('button');
// btn.addEventListener('click',(e)=>{
// console.log(e.target.innerText)
// })
let btns=document.querySelectorAll('.button');
btns.forEach((btn)=>{
btn.addEventListener("click",(e)=>{
console.log(e.target.innerText)
})
})
let heading = document.querySelector('h1')
heading.addEventListener('click',()=>{
console.log('heading is clicked')
})
Output :-
Comments
Post a Comment