forked from gdi-curriculum-review/gdi-jquery-intro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class2.html
1029 lines (941 loc) · 34.9 KB
/
class2.html
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Class 2 ~ Javascript ~ Girl Develop IT</title>
<meta name="description" content="This is the official Girl Develop It Core Intro Javascript course. Material based on original material by Sara Chipps, Pamela Fox, Alexis Goldstein, Izzy Johnston and Leo Newball.
The course is meant to be taught in 4 two-hour sections. Each of the slides and practice files are customizable according to the needs of a given class or audience.">
<meta name="author" content="Girl Develop It">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<link rel="stylesheet" href="reveal/css/reveal.css">
<link rel="stylesheet" href="reveal/css/theme/gdidefault.css" id="theme">
<!-- For syntax highlighting -->
<!-- light editor<link rel="stylesheet" href="lib/css/light.css">-->
<!-- dark editor--><link rel="stylesheet" href="reveal/lib/css/dark.css">
<!-- If use the PDF print sheet so students can print slides-->
<link rel="stylesheet" href="reveal/css/print/pdf.css" type="text/css" media="print">
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<!-- Opening slide -->
<section>
<img src = "images/gdi_logo_badge.png">
<h3>Beginning Javascript</h3>
<h4>Class 2</h4>
</section>
<!-- Welcome-->
<section>
<h3>Welcome!</h3>
<div class = "left-align">
<p>Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.</p>
<p class ="green">Some "rules"</p>
<ul>
<li>We are here for you!</li>
<li>Every question is important</li>
<li>Help each other</li>
<li>Have fun</li>
</ul>
</div>
</section>
<section>
<h3>Goals for the class</h3>
<ul>
<li>Week 1: Intro to JavaScript</li>
<li><strong>Week 2: More JavaScript & the DOM</strong></li>
<li>Week 3: Intro to jQuery</li>
<li>Week 4: AJAX and APIs</li>
</ul>
</section>
<section>
<h3>Useful References</h3>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/JavaScript">Mozilla Developer Network - JavaScript</a></li>
<li><a href="https://developer.mozilla.org/en-US/docs/DOM">Mozilla Developer Network - Dom</a></li>
<li><a href="http://www.w3.org/standards/webdesign/script">W3C JavaScript Specifications</a></li>
</ul>
</section>
<!-- Functions-->
<section>
<h3>Functions</h3>
<p>Functions are re-usable <br/>blocks (collections of statements)</p>
<div class = "fragment">
Declare a function
<pre><code contenteditable class ="javascript">
function sayHi(){
console.log('Hi!');
}
</code></pre>
</div>
<div class = "fragment">
Call the function
<pre><code contenteditable class ="javascript">
sayHi();
</code></pre>
</div>
</section>
<!-- Functions cont.-->
<section>
<h3>Arguments</h3>
<p class = "fragment">Functions can take named arguments</p>
<div class = "fragment">
<pre><code contenteditable class ="javascript">
function sayHi(name){
console.log('Hi' + name + '!');
}
sayHi('Jane');
sayHi('John');
// Pass variables as arguments
var name = 'Pip, the mouse';
sayHi(name);
</code></pre>
</div>
</section>
<!-- Functions cont.-->
<section>
<h3>Arguments</h3>
<p class = "fragment">Functions can take multiple named arguments</p>
<div class = "fragment">
<pre><code contenteditable class ="javascript">
function addNumbers(num1, num2){
var result = num1 + num2;
console.log(result);
}
addNumbers(5, 6);
var number1 = 12;
var number2 = 15;
addNumbers(number1, number2);
</code></pre>
</div>
</section>
<!-- Functions cont.-->
<section>
<h3>Return values</h3>
<p class = "fragment">Functions can return a value</p>
<div class = "fragment">
<pre><code contenteditable class ="javascript">
function addNumbers(num1, num2){
var result = num1 + num2;
return result; //Anything after this line won't be read
}
var sum = addNumbers(5, 6);
</code></pre>
</div>
</section>
<!-- Functions cont.-->
<section>
<h3>Variable Scope</h3>
<p>The context where a specific variable is valid</p>
<p>JavaScript has two scopes:</p>
<ul>
<li><strong>Local</strong> - available in the function where it is defined</li>
<li><strong>Global</strong> - available everywhere</li>
</ul>
</section>
<section>
<h3>Variable Scope: Local</h3>
<p>Available in the function where it is defined</p>
<pre><code contenteditable class ="javascript">
function addNumbers(num1, num2) {
var result = num1 + num2; // local
return result;
}
var sum = addNumbers(5, 6);
console.log(result); // undefined because it is outside of scope
</code></pre>
</section>
<section>
<h3>Variable Scope: Global</h3>
<p>Available everywhere</p>
<pre><code contenteditable class ="javascript">
var result; // global
function addNumbers(num1, num2) {
result = num1 + num2;
return result;
}
var sum = addNumbers(5, 6);
console.log(result); // outputs 11 because it is global
</code></pre>
</section>
<section>
<h3>Variable Scope: Best Practice</h3>
<p>Declare your variables at the top of their scope<br/> to avoid problems.</p>
<pre><code contenteditable class ="javascript">
function myFunction() {
// declare your variables at the top
var i, j;
var number = 10;
var string = "some string";
// code goes here
}
</code></pre>
</section>
<!-- Exercise-->
<section data-state="activity">
<section>
<h3>Let's Develop It: Functions</h3>
<ul>
<li>Wrap the favorite activity calculator in a function called
<code>calculateActivity</code></li>
<li><code>calculateActivity</code> should take an argument for the
name of the activity</li>
<li><code>calculateActivity</code> should take an argument for the
times you do the activity per month</li>
<li>Call <code>calculateActivity</code></li>
</ul>
</section>
<section>
<h3>Example</h3>
<p>Are you done or really stuck?</p>
<p>Check out <a href="class2/01-activity-function/index.html">our example</a>.</p>
</section>
</section>
<section>
<h3>Adding JS to HTML: Inline</h3>
<pre><code contenteditable class="html">
<html>
<head>
<title>My Site!</title>
</head>
<body>
My site!
<a href="#" onclick="alert('Hello World!'); return false;">
click me
</a>
</body>
</html>
</code></pre>
<p class="fragment">Runs JavaScript when the event (click) occurs.</p>
<p class="fragment">We'll talk about other events later.</p>
<p class="fragment">Returning false prevents the default action.</p>
</section>
<!-- Exercise-->
<section data-state="activity">
<section>
<h3>Let's Develop It: Inline Events</h3>
<p>Let's share information about another favorite activity</p>
<ul>
<li>Add a link to your html page</li>
<li>Set the <code>href</code> attribute to <code>#</code></li>
<li>Add an onclick event to the link that calls <code>calculateActivity</code>
with arguments for a different activity.</li>
<li>Include <code>return false</code> if you want to prevent
default behavior</li>
</ul>
</section>
<section>
<h3>Example</h3>
<p>Are you done or really stuck?</p>
<p>Check out <a href="class2/02-inline-event/index.html">our example</a>.</p>
</section>
</section>
<!-- Loops-->
<section>
<h3>Loops</h3>
<p>Sometimes you want to go through a piece of code multiple times</p>
<p class = "fragment">Why?</p>
<ul>
<li class = "fragment">Showing a timer count down</li>
<li class = "fragment">Displaying the results of a search</li>
<li class = "fragment">Adding images to a slideshow</li>
</ul>
</section>
<section>
<h3>The while loop</h3>
<p>The while loop tells JS to repeat statements <br/>while a condition is true:</p>
<pre><code contenteditable class = "javascript">
while (expression) {
// statements to repeat
}
</code></pre>
<pre><code contenteditable class = "javascript">
var x = 0;
while (x < 5) {
console.log(x);
x++;
}
</code></pre>
<div class = "fragment">
Review: '++' means increment by 1!
</div>
</section>
<section>
<h3>The while loop</h3>
<pre><code contenteditable class = "javascript">
var x = 0;
while (x < 5) {
console.log(x);
x++;
}
</code></pre>
<div class = "fragment">
<div class = "yellow">Danger!!</div>
<p>What happens if we forget x++;?</p>
<p>The loop will never end!!</p>
</div>
</section>
<section>
<h3>The for loop</h3>
<p>The for loop is a safer way of looping</p>
<pre><code contenteditable class = "javascript">
for (initialize; condition; update) {
// statements to repeat
}
</code></pre>
<pre><code contenteditable class = "javascript">
for (var i = 0; i < 5; i++) {
console.log(i);
}
</code></pre>
<div class = "fragment">
Less danger of an infinite loop. <br/>All conditions are at the top!
</div>
</section>
<!-- Arrays -->
<section>
<h3>Array</h3>
<p>An array is a data-type that holds <br/>an ordered list of values of any type:</p>
<div class = "fragment">
<pre><code contenteditable class = "javascript">
var arrayName = [element0, element1, ...];
</code></pre>
</div>
<div class = "fragment">
<pre><code contenteditable class = "javascript">
var favoriteNumbers = [16, 27, 88];
var rainbowColors = ['Red', 'Orange', 'Yellow', 'Green',
'Blue', 'Indigo', 'Violet'];
var luckyThings = ['Rainbows', 7, 'Horseshoes'];
</code></pre>
</div>
<div class = "fragment">
The length property reports the size of the array:
<pre><code contenteditable class = "javascript">
console.log(rainbowColors.length);
</code></pre>
</div>
</section>
<section>
<h3>Arrays: accessing values</h3>
<p class = "fragment">You can access items with "bracket notation".</p>
<div class = "fragment">
The number inside the brackets is called an "index"
<pre><code contenteditable class = "javascript">
var arrayItem = arrayName[indexNum];
</code></pre>
</div>
<div class = "fragment">
<p>Arrays in JavaScript are <em>zero-indexed</em>, <br/>which means
we start counting from zero.</p>
<pre><code contenteditable class = "javascript">
var colors = ['Red', 'Green', 'Blue'];
var firstColor = rainbowColors[0]; // Red
var lastColor = rainbowColors[2]; // Blue
</code></pre>
</div>
</section>
<section>
<h3>Arrays: updating values</h3>
<div class = "fragment">
You can also use bracket notation to <br/>change the item in an array:
<pre><code contenteditable class = "javascript">
var awesomeAnimals = ['Corgis', 'Otters', 'Octopi'];
awesomeAnimals[0] = 'Bunnies';
</code></pre>
</div>
<div class = "fragment">
Or to add to an array:
<pre><code contenteditable class = "javascript">
awesomeAnimals[3] = 'Corgis';
</code></pre>
</div>
<div class = "fragment">
You can also use the push method:
<pre><code contenteditable class = "javascript">
awesomeAnimals.push('Ocelots');
</code></pre>
</div>
</section>
<section>
<h3>Loops and Arrays</h3>
Use a for loop to easily look at each item in an array:
<pre><code contenteditable class = "javascript">
var rainbowColors = ['Red', 'Orange', 'Yellow', 'Green',
'Blue', 'Indigo', 'Violet'];
for (var i = 0; i < rainbowColors.length; i++) {
console.log("Index: " + i)
console.log("Value: " + rainbowColors[i]);
}
</code></pre>
</section>
<!-- Exercise -->
<section data-state="activity">
<section>
<h3>Let's Develop It: Favorite Things</h3>
<p>Output a list of your favorite things.</p>
<ul>
<li>Add a new link to your html page with an onclick that calls
the function <code>favoriteThings</code></li>
<li>Create a JS function called <code>favoriteThings</code></li>
<li>Create an array with some of your favorite things</li>
<li>Use a for loop to generate a sentence about your favorite things:
"My favorite things are XX, YY, ZZ"</li>
<li>Output the sentence</li>
</ul>
</section>
<section>
<h3>Let's Develop It: Favorite Things</h3>
<p>Output a list of your favorite things.</p>
<ul>
<li>Bonus: add an 'and' in before the last item:
<br/>"My favorite things are XX, YY, and ZZ"</li>
</ul>
</section>
<section>
<img src="images/favorite-things.png"/>
</section>
<section>
<h3>Hints</h3>
<ul>
<li>Remember arrays are zero-indexed.</li>
<li>You need to use string concatenation to build your sentence.</li>
<li>You probably need an if/else to do the bonus.</li>
</ul>
</section>
<section>
<h3>Example</h3>
<p>Are you done or really stuck?</p>
<p>Check out <a href="class2/03-favorite-things/index.html">our example</a>.</p>
</section>
</section>
<!-- Objects-->
<section>
<h3>Objects</h3>
<p>A data type that stores a collection of properties.</p>
<p>A property is an association between a name and value.</p>
<pre><code contenteditable class = "javascript">
var objectName = {
propertyName: propertyValue,
propertyName: propertyValue,
...
};
</code></pre>
<div class = "fragment">
<pre><code contenteditable class = "javascript">
var charlie = {
age: 8,
name: "Charlie Brown",
likes: ["baseball", "The little red-haired girl"],
pet: "Snoopy"
};
</code></pre>
</div>
</section>
<section>
<h3>Objects: accessing values</h3>
<p>Access values of "properties" using "dot notation":</p>
<div class = "fragment">
<pre><code contenteditable class = "javascript">
var charlie = {
age: 8,
name: "Charlie Brown",
likes: ["baseball", "The little red-haired girl"],
pet: "Snoopy"
};
</code></pre>
</div>
<div class = "fragment">
<pre><code contenteditable class = "javascript">
var pet = charlie.pet;
</code></pre>
</div>
</section>
<section>
<h3>Objects: accessing values</h3>
<div class = "fragment">
Or using "bracket notation" (like arrays):
<pre><code contenteditable class = "javascript">
var name = charlie['name'];
</code></pre>
</div>
<div class = "fragment">
Non-existent properties will return undefined:
<pre><code contenteditable class = "javascript">
var hometown = charlie.hometown;
</code></pre>
</div>
</section>
<section>
<h3>Objects: changing values</h3>
<p>Use dot or bracket notation with the assignment operator to change objects.</p>
<div class = "fragment">
Change existing properties:
<pre><code contenteditable class = "javascript">
charlie.name = "Chuck";
</code></pre>
</div>
<div class = "fragment">
Or add new properties:
<pre><code contenteditable class = "javascript">
charlie.hometown = "Peanuts";
</code></pre>
</div>
<div class ="fragment">
You can also delete properties:
<pre><code contenteditable class = "javascript">
delete charlie.hometown;
</code></pre>
</div>
</section>
<section>
<h3>Arrays of Objects</h3>
<p>Arrays can hold objects too!</p>
<div class = "fragment">
<pre><code contenteditable class = "javascript"> var peanuts = [
{
name: "Charlie Brown",
pet: "Snoopy"
},
{
name: "Linus van Pelt",
pet: "Blue Blanket"
}
];</code></pre>
</div>
<div class = "fragment">
That means we can use a for loop!
<pre><code contenteditable class = "javascript"> for (var i = 0; i < peanuts.length; i++) {
var peanut = peanuts[i];
console.log(peanut.name + ' has a pet named ' +
peanut.pet + '.');
}</code></pre>
</div>
</section>
<section>
<h3>Objects in functions</h3>
<p>You can pass an object into a function as a parameter</p>
<pre><code contenteditable class = "javascript">
var peanut = {
name: "Charlie Brown",
pet: "Snoopy"
};
</code></pre>
<div class = "fragment">
<pre><code contenteditable class = "javascript">
function describeCharacter(character){
console.log(character.name + ' has a pet named ' +
character.pet + '.');
}
</code></pre>
</div>
<div class = "fragment">
<pre><code contenteditable class = "javascript">
describeCharacter(peanut);
</code></pre>
</div>
</section>
<!-- Exercise -->
<section data-state="activity">
<section>
<h3>Let's Develop It: My Connections</h3>
<p>Share your connections (friends, pets, etc.).</p>
<ul>
<li>Add a link that calls the function <code>myConnections</code> onclick</li>
<li>Add a <code>myConnections</code> javascript function</li>
<li>Create an array of connection objects, with their name, type (e.g. friend),
and something they like.</li>
<li>Use a loop to go through each connection and describe them</li>
<li>Alert the results</li>
</ul>
</section>
<section>
<h3>Let's Develop It: My Connections</h3>
<p>Share your connections (friends, pets, etc.).</p>
<ul>
<li>Bonus: make a separate function that describes a connection</li>
</ul>
</section>
<section>
<img src="images/my-connections.png"/>
</section>
<section>
<h3>Hints</h3>
<ul>
<li>Use "\n" to insert a new line, if you want one.</li>
<li>Remember arrays are zero-indexed.</li>
<li>You need to use string concatenation to build your description.</li>
<li>For the bonus, use <code>return</code> to return the description.</li>
</ul>
</section>
<section>
<h3>Example</h3>
<p>Are you done or really stuck?</p>
<p>Check out <a href="class2/04-my-connections/index.html">our example</a>.</p>
</section>
</section>
<!-- DOM -->
<section>
<h3>DOM</h3>
<p>Document Object Model</p>
<ul>
<li class="fragment">A structural representation of the HTML elements on a web page</li>
<li class="fragment">A way to interact with the HTML elements <br/>on a web page</li>
<li class="fragment">Connects web pages to scripts or programming languages -
most commonly JavaScript</li>
</ul>
</section>
<section data-state="activity">
<h3>DOM</h3>
<p>Look at the DOM reprentation of
<a href="class2/04-my-connections/index.html">your page</a> in the
<strong>elements</strong> tab in the developer tools</p>
<img src = "images/dom.png">
</section>
<section>
<p>In the DOM, web pages are like trees</p>
<pre><code contenteditable class = "html"> <!DOCTYPE html>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>My Page</h1>
<p>Some Text.</p>
</body>
</html></code></pre>
<img class="fragment" src="images/dom-tree.png" height="300"/>
</section>
<section>
<h3>DOM: document</h3>
<p>Entry point for the web page's content. <br/>The root of the tree.</p>
<img src="images/dom-tree-document.png"/>
</section>
<section>
<h3>DOM: element</h3>
<p>HTML nodes in the tree.</p>
<img src="images/dom-tree-elements.png"/>
</section>
<section>
<h3>DOM: text</h3>
<p>Text nodes in the tree.</p>
<img src="images/dom-tree-text.png"/>
</section>
<section data-state="activity">
<h3>DOM: document</h3>
<p><a href="class2/dom-trial.html">Take a look</a> at the tree.</p>
<img src="images/document-console.png"/>
</section>
<section>
<section>
<h3>DOM traversal: body</h3>
<p>Get the body of your html</p>
<pre><code contenteditable class = "javascript">
document.body;
</code></pre>
</section>
<section data-state="activity">
<h3>DOM traversal: body</h3>
<p><a href="class2/dom-trial.html">Try it!</a></p>
<img src="images/document-body.png"/>
</section>
</section>
<section>
<section>
<h3>DOM traversal: parent</h3>
<p>Get the parent of a node.</p>
<pre><code contenteditable class = "javascript">
document.body.parentNode;
</code></pre>
<div class="fragment">
<p>Go up the tree</p>
<img src="images/tree-parentnode.png"/>
</div>
</section>
<section data-state="activity">
<h3>DOM traversal: parent</h3>
<p><a href="class2/dom-trial.html">Try it!</a></p>
<img src="images/parent-node.png"/>
</section>
</section>
<section>
<section>
<h3>DOM traversal: children</h3>
<p>Get the children of a node.</p>
<pre><code contenteditable class = "javascript">
var children = document.body.childNodes; // Array of child nodes
for(var i = 0; i < children.length; i++) {
var child = children[i]; // A child node
}
</code></pre>
<div class="fragment">
<p>Go down the tree</p>
<img src="images/tree-childnodes.png" width="400"/>
</div>
</section>
<section data-state="activity">
<h3>DOM traversal: children</h3>
<p><a href="class2/dom-trial.html">Try it!</a></p>
<img src="images/child-nodes.png"/>
<p class="fragment">Sometimes whitespace leads to extra text nodes</p>
</section>
</section>
<section>
<h3>DOM traversal: children</h3>
<p>Get just the first child.</p>
<pre><code contenteditable class = "javascript">
document.body.firstChild;
</code></pre>
</section>
<section>
<section>
<h3>DOM traversal: siblings</h3>
<p>Get the siblings of a node.</p>
<pre><code contenteditable class = "javascript">
var node = document.body.firstChild;
var next = node.nextSibling;
var previous = next.previousSibling;
</code></pre>
<div class="fragment">
<p>Go across a branch of the tree</p>
<img src="images/tree-siblings.png" height="300"/>
</div>
</section>
<section data-state="activity">
<h3>DOM traversal: siblings</h3>
<p><a href="class2/dom-trial.html">Try it!</a></p>
<img src="images/siblings.png"/>
</section>
</section>
<section>
<h3>DOM searching</h3>
<p class="left-align blue">Finding every element on the page by traversing the tree is time consuming!</p>
<p class="fragment left-align">The document object also provides methods for finding DOM nodes without going one by one.</p>
</section>
<section>
<section>
<h3>DOM searching: id</h3>
<p>Get an element by its id</p>
<pre><code contenteditable class = "html">
<div id="my-data">
the data
</div>
</code></pre>
<pre><code contenteditable class = "javascript">
document.getElementById('my-data');
</code></pre>
<p class="fragment">Ids are unique, so there should only be one</p>
</section>
<section data-state="activity">
<h3>DOM searching: id</h3>
<p><a href="class2/dom-trial.html">Try it!</a></p>
<img src="images/search-id.png"/>
</section>
</section>
<section>
<section>
<h3>DOM searching: tag name</h3>
<p>Get elements by their tag name</p>
<pre><code contenteditable class = "html">
<ul>
<li>item 1</li>
<li>item 2</li>
<li>item 3</li>
</ul>
</code></pre>
<pre><code contenteditable class = "javascript">
// Array of elements
var items = document.getElementsByTagName('li');
for(var i = 0; i < items.length; i++) {
var item = items[i];
}
</code></pre>
</section>
<section data-state="activity">
<h3>DOM searching: id</h3>
<p><a href="class2/dom-trial.html">Try it!</a></p>
<img src="images/search-tags.png"/>
</section>
</section>
<section>
<h3>DOM searching: tag name</h3>
<p>Limit the scope of the search</p>
<pre><code contenteditable class="html">
<div id="important">
<p>important text</p>
</div>
<div>
<p>other text</p>
</div>
</code></pre>
<pre><code contenteditable class="javascript">
// All p elements
var allParagraphs = document.getElementsByTagName('p');
var scope = document.getElementById('important');
// p elements that are children of the scope element
var paragraphs = scope.getElementsByTagName('p');
</code></pre>
</section>
<section>
<section>
<h3>Dom Node: attributes</h3>
<p>You can get and set node attributes</p>
<pre><code contenteditable class="html">
<a id="gdi-link" href="http://girldevelopit.com">
Girl Develop It
</a>
</code></pre>
<pre><code contenteditable class="javascript">
var link = document.getElementById('gdi-link');
link.getAttribute('href'); // get attribute
var pghLink = 'http://girldevelopit.com/chapters/pittsburgh';
link.setAttribute('href', pghLink); //set attribute
</code></pre>
</section>
<section data-state="activity">
<h3>Dom Node: attributes</h3>
<p><a href="class2/dom-trial.html">Try it!</a></p>
<img src="images/dom-attributes.png"/>
</section>
</section>
<section>
<section>
<h3>Dom Node: innerHTML</h3>
<p>Each node has an innerHTML property that can be used to get and
set the content of the node.</p>
<pre><code contenteditable class="html">
<div id="important">
<p>important text</p>
</div>
</code></pre>
<pre><code contenteditable class="javascript">
var important = document.getElementById('important');
important.innerHTML; // get content
var newContent = '<strong>IMPORTANT TEXT!!!</strong>';
important.innerHTML = newContent; //set content
important.innerHTML += 'more text!'; // add to content
</code></pre>
<p class="fragment">You can also add to the content.</p>
</section>
<section data-state="activity">
<h3>Dom Node: innerHTML</h3>
<p><a href="class2/dom-trial.html">Try it!</a></p>
<img src="images/inner-html.png"/>
</section>
</section>
<section>
<section>
<h3>Dom: creating nodes</h3>
<p>The document object can create new nodes.</p>
<pre><code contenteditable class="javascript">
document.createElement(tagName);
document.createTextNode(text);
document.appendChild();
</code></pre>
<div class = "fragment">
<pre><code contenteditable class = "javascript">
var newParagraph = document.createElement('p');
var paragraphText = document.createTextNode('My new text!');
newParagraph.appendChild(paragraphText);
document.body.appendChild(newParagraph);
</code></pre>
</section>
<section data-state="activity">
<h3>Dom Node: creating nodes</h3>
<p><a href="class2/dom-trial.html">Try it!</a></p>
<img src="images/dom-create-nodes.png"/>
</section>
</section>
<section>
<h3>DOM: window</h3>
<p>Represents the window</p>
<pre><code contenteditable class = "javascript">
window.document;
// How far has the user scrolled
window.scrollX;
window.scrollY;
// How big is the window
window.innerWidth;
window.innerHeight;
</code></pre>
</section>
<section>
<h3>DOM: window</h3>
<p class="left-align">We don't have access to the DOM until the window loads.</p>
<p class="left-align fragment">Immediately running JS that depends on the DOM leads to problems.</p>
<pre class="fragment"><code contenteditable class = "javascript">
window.onload = function() {
// Put JS that runs on page load here
}
</code></pre>
</section>
<!-- Exercise-->
<section data-state="activity">
<section>
<h3>Let's Develop It</h3>
<ul>
<li>Put it all together</li>
<li>Modify your existing code to add new elements to the
screen instead of firing an alert or outputting to console.log</li>
<li>You can change the HTML too.</li>
<li>Keep in mind how to find an element, how to append an element, and how to change the inner html of an element</li>
<li>There are lots of possible solutions! Be creative!</li>
</ul>
</section>
<section>
<h3>Hints</h3>
<ul>
<li>It can be easier to find a DOM element if it has an id.</li>
<li>The welcome message code needs to wait for the window to load
before accessing the DOM.</li>
</ul>
</section>
<section>
<h3>Example</h3>
<p>Are you done or really stuck?</p>
<p>Check out <a href="class2/05-dom/index.html">our example</a>.</p>
<p>Keep in mind that this is one of many possible solutions.</p>
</section>
</section>
<!-- -->
<section>
<h2>Questions?</h2>
<div style = "font-size:1200%; height:100%; margin-top:40%" class ="blue">?
<div class ="clear"></div></div>
</section>
</div>
<footer>
<div class="copyright">
Javascript ~ Girl Develop It ~
<a rel="license" href="http://creativecommons.org/licenses/by-nc/3.0/deed.en_US"><img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by-nc/3.0/80x15.png" /></a>
</div>
</footer>
</div>
<script src="reveal/lib/js/head.min.js"></script>