This repository was archived by the owner on Apr 8, 2025. It is now read-only.
File tree 5 files changed +48
-27
lines changed
5 files changed +48
-27
lines changed Original file line number Diff line number Diff line change 2
2
< head >
3
3
< title > My First JS</ title >
4
4
< script type ="text/javascript ">
5
- function sayHello ( ) {
6
- alert ( 'Hello from JavaScript!' ) ;
7
- console . log ( 'hello in the console' ) ;
5
+ function setUpClickability ( ) {
6
+ var button = document . getElementById ( 'mybutton' )
7
+ button . addEventListener ( 'click' , function ( ) {
8
+ alert ( 'clicked! thank you!' )
9
+ console . log ( 'clicked' )
10
+ } ) ;
8
11
}
9
- document . addEventListener ( 'DOMContentLoaded' , function ( ) {
10
- document . getElementById ( 'mybutton' ) . addEventListener ( 'click' , sayHello ) ;
11
- } ) ;
12
+ document . addEventListener ( 'DOMContentLoaded' , setUpClickability ) ;
12
13
</ script >
13
14
</ head >
14
15
< body >
Original file line number Diff line number Diff line change 4
4
< script type ="text/javascript ">
5
5
document . addEventListener ( 'DOMContentLoaded' , function ( ) {
6
6
7
- // click list
8
- var clicks = 0 ;
9
- var click_list = document . getElementById ( 'clicklist' ) ;
10
- function addClick ( ) {
11
- clicks += 1 ;
7
+ var button = document . getElementById ( 'mybutton' ) ;
8
+ var clicklist = document . getElementById ( 'clicklist' ) ;
9
+
10
+ button . addEventListener ( 'click' , function ( ) {
11
+
12
12
var li = document . createElement ( 'li' ) ;
13
- li . innerText = 'click #' + clicks . toString ( ) ;
14
- click_list . appendChild ( li ) ;
15
- }
13
+ li . innerText = 'click!'
14
+ clicklist . appendChild ( li )
16
15
17
- document . getElementById ( 'mybutton' ) . addEventListener ( 'click' , addClick ) ;
16
+ } ) ;
18
17
19
18
} ) ;
20
19
</ script >
23
22
< p >
24
23
< button id ="mybutton "> Click Me</ button >
25
24
</ p >
26
- < ul id ="clicklist "> </ ul >
25
+ < ul id ="clicklist ">
26
+ < li > first bullet</ li >
27
+ < li > second bullet</ li >
28
+ </ ul >
27
29
</ body >
28
30
</ html >
Original file line number Diff line number Diff line change 3
3
< title > My First JS</ title >
4
4
< script type ="text/javascript ">
5
5
document . addEventListener ( 'DOMContentLoaded' , function ( ) {
6
- // chars in text box
6
+
7
7
var text_box = document . getElementById ( 'text_box' ) ;
8
8
var chars_display = document . getElementById ( 'chars_display' ) ;
9
- function updateDisplay ( ) {
10
- chars_display . innerText = text_box . value . length ;
11
- }
12
- text_box . addEventListener ( 'keyup' , updateDisplay ) ;
9
+
10
+ text_box . addEventListener ( 'keyup' , function ( ) {
11
+ chars_display . innerText = text_box . value . length
12
+ } )
13
+
13
14
} ) ;
14
15
</ script >
15
16
</ head >
16
17
< body >
17
18
< p >
18
- < input type ="text " id ="text_box "/> Chars: < span id ="chars_display "> </ span >
19
+ < input type ="text " id ="text_box "/>
20
+ Chars:
21
+ < span id ="chars_display "> </ span >
19
22
</ p >
20
23
</ body >
21
24
</ html >
Original file line number Diff line number Diff line change 1
1
{
2
- "next_post_id" : 1 ,
2
+ "next_post_id" : 3 ,
3
3
"posts" : [
4
4
{
5
5
"title" : " Foo" ,
6
6
"body" : " bar" ,
7
- "likes" : 1 ,
7
+ "likes" : 2 ,
8
8
"id" : 0
9
+ },
10
+ {
11
+ "title" : " My thoughtful post" ,
12
+ "body" : " bla bla bla" ,
13
+ "likes" : 2 ,
14
+ "id" : 1
15
+ },
16
+ {
17
+ "title" : " bla bla" ,
18
+ "body" : " asdlfkjasdf" ,
19
+ "likes" : 2 ,
20
+ "id" : 2
9
21
}
10
22
]
11
23
}
Original file line number Diff line number Diff line change @@ -15,13 +15,16 @@ function install_like_handler(like_button) {
15
15
} else {
16
16
command = 'unlike' ;
17
17
}
18
- // AJAX magic!
18
+ // AJAX magic goes here
19
+
19
20
var req = new XMLHttpRequest ( ) ;
20
- req . open ( 'get' , '/' + command + '/' + post_id , true ) ; // e.g. /like/2
21
+ req . open ( 'get' , '/' + command + '/' + post_id , true ) ;
21
22
req . addEventListener ( 'load' , function ( ) {
22
- update_like_number ( like_button )
23
+ update_like_number ( like_button ) ;
23
24
} ) ;
24
25
req . send ( ) ;
26
+
27
+ console . log ( command , post_id ) ;
25
28
} ) ;
26
29
}
27
30
You can’t perform that action at this time.
0 commit comments