forked from eda-old-challenges/event-handling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents.js
61 lines (55 loc) · 1.74 KB
/
events.js
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Don't change or delete this line! It waits until the DOM has loaded, then calls
// the start function. More info:
// https://developer.mozilla.org/en-US/docs/Web/Events/DOMContentLoaded
document.addEventListener('DOMContentLoaded', start)
function start () {
// The first example is done for you. This will change the background colour of the first div
// when you mouse over it.
one()
two()
three()
four()
// Your turn! Create a new function called `two`, then call it from here.
}
function one () {
// First, we have to find the element:
var one = document.getElementById('one')
// Next, we add an event listener to it:
one.addEventListener('mouseenter', makeBlue)
// Finally, we add one to make the colour white again
one.addEventListener('mouseleave', makeWhite)
}
// CREATE FUNCTION two HERE
function two(){
var two= document.getElementById('two')
two.addEventListener('mouseenter', makeGreen)
two.addEventListener('mouseleave', makeWhite)
}
// CREATE FUNCTION three HERE
function three(){
var three= document.getElementById('three')
three.addEventListener('mouseenter', makeOrange)
three.addEventListener('mouseleave', makeWhite)
}
// CREATE FUNCTION four HERE
function four(){
var four= document.getElementById('four')
four.addEventListener('click', makePink)
four.addEventListener('mouseleave', makeWhite)
}
// Changes the background color of event's target
function makeBlue (evt) {
evt.target.style.backgroundColor = 'blue'
}
function makeWhite (evt) {
evt.target.style.backgroundColor = 'white'
}
function makeGreen (evt){
evt.target.style.backgroundColor = 'green'
}
function makeOrange (evt){
evt.target.style.backgroundColor = 'orange'
}
function makePink (evt){
evt.target.style.backgroundColor = 'pink'
}