Event & Eventlisner
Event & Eventlisner
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, initial-
scale=1.0">
<title>Document</title>
</head>
<body>
<h1
onclick="console.log('Hi')">hola9</h1>
<h2 onclick="clicked()">Pune</h2>
<p id="p1">This is para1</p>
<p id="p2">This is para2</p>
<p id="p3">This is para3</p>
<p id="p4">This is para4</p>
<button id="btn1">Click</button>
<p id="para">Good Evening</p>
<script src="event.js"></script>
</body>
</html>
/*
***Events***:
- Events are actions performed by users on
html elements which can be taken care of by
using event handlers.
***addEventListene() Method:
- Add an event listener that fires when a
user click a button.
- The addEventListener() method attaches an
event handler to the specified element.
*/
function clicked() {
console.log("click");
}
let a = document.getElementById("p1");
let b = a.addEventListener("click", function
() {
a.style.color = "blue";
});
//double click
let a1 = document.getElementById("p2");
let b1 = a1.addEventListener("dblclick",
function () {
a1.style.color = "blue";
});
//changes on mouseover
let a2 = document.getElementById("p3");
let b2 = a2.addEventListener("mouseover",
function () {
a2.style.backgroundColor = "yellow";
console.log("mousover");
});
//changes on mouse right click
let a3 = document.getElementById("p4");
let b3 = a3.addEventListener("contextmenu",
function () {
a3.style.backgroundColor = "red";
console.log("right click in mouse");
});
//light/dark mode
let modebtn =
document.getElementById("btn1");
let currmode = "light";
let body = document.querySelector("body");
let para = document.querySelector("#para");
modebtn.addEventListener("click", function ()
{
if (currmode === "light") {
currmode = "dark";
body.style.backgroundColor = "black";
body.style.color = "white";
} else {
currmode = "light";
body.style.backgroundColor = "white";
body.style.color = "black";
}
});