1/* This example assumes you have two elements with an id
2 attribute of 'cats' and 'dogs'.
3*/
4
5const conditionOneHandler = event => {
6 alert('I like cats!');
7}
8
9const conditionTwoHandler = event => {
10 alert('I like dogs!');
11}
12
13const clickHandler = (event) => {
14
15 // Do whatever. You can use the target object
16 // from the event to conditionally handle the
17 // event.
18 switch (event.target.id) {
19 default: return;
20 case 'dogs': return conditionOneHandler(event);
21 case 'cats': return conditionTwoHandler(event);
22
23 }
24
25}
26
27/*
28 Attach the click event listener to the element.
29 You can also use a comma after each line to declare
30 multiple variables of the same type, instead of
31 writing 'const' every time. :)
32*/
33
34const dogsEl = document.querySelector('#dogs'),
35 catsEl = document.querySelector('#cats'),
36 dogsEl.onclick = event => clickHandler(event),
37 catsEl.onclick = event => clickHandler(event)