-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path9-33. Events.js
More file actions
32 lines (27 loc) · 813 Bytes
/
9-33. Events.js
File metadata and controls
32 lines (27 loc) · 813 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Events :
// Example code (Here i am not uploading HTML and CSS file to Github of this code)
console.clear()
var btn = document.getElementById("btn-click");
btn.onclick = function(){
alert("Button Clicked");
}
// or
function onBtnClick(){
alert("Button Clicked");
}
btn.onclick = onBtnClick;
/* or */
btn.addEventListener('click', function(){
alert('Anonymous Function Called')
})
/* or */
btn.addEventListener('click', onBtnClick)
// Chnaging background Color :
function onBtnClick(){
mainDiv.style.backgroundColor = "lightcoral"
}
// or randomly change color of bg :
function onBtnClick(){
mainDiv.style.backgroundColor = "rgb(" + Math.floor(Math.random()*255)+ "," +
Math.floor(Math.random() * 255) + "," + Math.floor(Math.random() * 255) * ")"
}