File tree Expand file tree Collapse file tree 1 file changed +86
-0
lines changed
Expand file tree Collapse file tree 1 file changed +86
-0
lines changed Original file line number Diff line number Diff line change 1+ <!DOCTYPE html>
2+ < html lang ="en ">
3+ < head >
4+ < meta charset ="UTF-8 ">
5+ < meta http-equiv ="X-UA-Compatible " content ="IE=edge ">
6+ < meta name ="viewport " content ="width=device-width, initial-scale=1.0 ">
7+ < title > Simple Example of Event Listener</ title >
8+ < style >
9+ * {
10+ box-sizing : border-box;
11+ }
12+
13+ # container {
14+
15+
16+ margin-top : 45vh ;
17+ border : 2px red solid;
18+ display : flex ;
19+ flex-direction : column;
20+ gap : 20px ;
21+ justify-content : space-around;
22+ padding : 20px ;
23+ align-items : center;
24+
25+ }
26+ .btn_container {
27+ display : flex;
28+ justify-content : space-between;
29+ gap : 20px ;
30+ align-items : center;
31+ }
32+ .btn_container : first-child {
33+ color : red;
34+ }
35+ .btn_container : nth-child (2 ){
36+ color : blue;
37+ }
38+ button {
39+ width : 50% ;
40+ margin : auto;
41+ background-color : azure;
42+ border-radius : 10px ;
43+ width : 100px ;
44+ height : 50px ;
45+ font-size : larger;
46+ }
47+ </ style >
48+ </ head >
49+ < body >
50+ < div id ="container ">
51+ < h1 > Simple Example of Event Listener</ h1 >
52+ click the button to see the event listener in action:
53+
54+ < div class ="btn_container ">
55+ < button id ="btn-red "> Red!</ button >
56+
57+
58+ < button id ="btn-blue "> Blue</ button >
59+ </ div >
60+
61+ </ div >
62+ < script >
63+ // Select the button element
64+ const buttonRed = document . getElementById ( "btn-red" ) ;
65+ const buttonBlue = document . getElementById ( "btn-blue" ) ;
66+ const bg = document . getElementById ( "container" ) ;
67+
68+ // Add an event listener to the button
69+ buttonRed . addEventListener ( "click" , function ( ) {
70+ bg . style . backgroundColor = "red" ;
71+ } ) ;
72+ buttonBlue . addEventListener ( "click" , function ( ) {
73+ bg . style . backgroundColor = "blue"
74+ } )
75+
76+
77+ </ script >
78+ </ body >
79+
80+
81+
82+
83+
84+
85+
86+ </ html >
You can’t perform that action at this time.
0 commit comments