forked from gdi-curriculum-review/gdi-jquery-intro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass3.html
950 lines (879 loc) · 34.5 KB
/
class3.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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Class 3 ~ 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 3</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>Week 2: More JavaScript & the DOM</li>
<li><strong>Week 3: Intro to jQuery</strong></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://api.jquery.com/">jQuery API</a></li>
<li><a href="http://www.w3.org/standards/webdesign/script">W3C JavaScript Specifications</a></li>
</ul>
</section>
<!-- What is a library-->
<section>
<h3>What is a library?</h3>
<ul>
<li class = "fragment">Software libraries hold functions (not books!)</li>
<li class = "fragment">When you include a library, you can use all the functions in that library</li>
<li class = "fragment">That means: <span class = "pink">you get to take advantage of other people's experience!</li>
<li class = "fragment">And... <span class ="blue">Save time!</span></li>
</ul>
</section>
<!-- What is jQuery-->
<section>
<h3>What is jQuery?</h3>
<p class = "fragment">jQuery is a <span class = "green">library</span> of JavaScript functions.</p>
<p class = "fragment">It contains many functions to help simplify your programming, including:</p>
<ul>
<li class = "fragment">HTML element selection & manipulation</li>
<li class = "fragment">CSS manipulation</li>
<li class = "fragment">HTML events</li>
<li class = "fragment">JavaScript effects and animations</li>
</ul>
</section>
<section>
<h3>Why use jQuery?</h3>
<ul>
<li class = "fragment">The most popular JavaScript library</li>
<li class = "fragment">jQuery empowers you to "write less, do more."</li>
<li class = "fragment">Great <a href = "http://docs.jquery.com/" target = "_blank">documentation and tutorials</a></li>
<li class="fragment">Works across a multitude of browsers</li>
<li class = "fragment">Used by nearly 20 million(!) websites</li>
</ul>
</section>
<section>
<h3>jQuery: A Brief History</h3>
<ul>
<li class = "fragment">jQuery was created by John Resig, a JavaScript tool developer at Mozilla.</li>
<li class = "fragment">January 2006: John announced jQuery at BarCampNYC: BarCampNYC Wrap-up</li>
<li class = "fragment">September 2007: A new user interface library is added to jQuery: jQuery UI: Interactions and Widgets</li>
<li class = "fragment">September 2008: Microsoft and Nokia announce their support for jQuery</li>
<li class = "fragment">December 2009: jQuery wins .Net Magazine's Award for Best Open Source Application</li>
</ul>
</section>
<!-- Including jquery-->
<section>
<h3>Including jQuery</h3>
Three main ways to include jQuery on your page.
</section>
<section>
<h3>Including jQuery: Local</h3>
<p>Download the library, store it locally:</p>
<pre><code contenteditable class = "html">
<head>
<script src="jquery.js"></script>
</head>
</code></pre>
</section>
<section>
<h3>Including jQuery: Hosted by jQuery</h3>
<p>Include the the live library from jQuery:</p>
<pre><code contenteditable class = "html">
<head>
<script src="http://code.jquery.com/jquery-1.9.1.min.js">
</script>
</head>
</code></pre>
</section>
<section>
<h3>Including jQuery: Hosted by Google</h3>
<p>Include the the live library from Google:</p>
<pre><code contenteditable class = "html">
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
</head>
</code></pre>
</section>
<section data-state="activity">
<section>
<h3>Let's Develop It</h3>
<h4>Add jQuery</h4>
<ul>
<li>Link to the Google hosted jQuery</li>
<li>Put it before your JavaScript file</li>
</ul>
</section>
<section>
<h3>Let's Develop It</h3>
<h4>Add jQuery</h4>
<pre><code class ="html">
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
<script src="scripts.js"></script>
</head>
</code></pre>
<p class="fragment">jQuery should go first because<br/> our code will
depend on it later.</p>
</section>
<section>
<h3>Example</h3>
<p>Are you done or really stuck?</p>
<p>Check out <a href="class3/01-include-jquery/index.html">our example</a>.</p>
<p>Keep in mind that this is one of many possible solutions.</p>
</section>
</section>
<section>
<h3>jQuery Basics</h3>
<p>Select some elements and do something with them.</p>
</section>
<section>
<h3>jQuery Basics</h3>
<p>The <a href="http://api.jquery.com/">jQuery API Documentation</a>
is a great reference.</p>
<br/>
<h4 class="fragment">API</h4>
<ul>
<li class="fragment">Stands for <strong>A</strong>pplication <strong>P</strong>rogramming
<strong>I</strong>nterface</li>
<li class="fragment">A set of routines, protocols, and tools for building software
applications.</li>
<li class="fragment">The building blocks that help a programmer <br/>build a program.</li>
</ul>
</section>
<section>
<h3>jQuery Selectors</h3>
<p class="left-align">Before we used things like <code>document.getElementById()</code>
and <code>document.getElementsByTagName()</code></p>
<br/>
<p class="fragment left-align">In jQuery, we find elements using <code class="blue">$()</code></p>
<p class="fragment left-align">In jQuery, we find elements using CSS selectors</p>
<p class="fragment left-align"><code>$()</code> is an alias for
<code>jQuery()</code></p>
</section>
<section>
<section>
<h3>jQuery Selectors: id</h3>
<p>Find elements by id using <code>#</code></p>
<pre><code contenteditable class = "html">
<div id="my-data">
the data
</div>
<div id="important">
<p>important text</p>
</div>
</code></pre>
<pre><code contenteditable class = "javascript">
$("#important");
$("#my-data");
</code></pre>
</section>
<section data-state="activity">
<h3>jQuery Selectors: id</h3>
<p><a href="class3/dom-trial.html">Try it!</a></p>
<img src="images/jquery-id.png"/>
</section>
</section>
<section>
<section>
<h3>jQuery Selectors: element</h3>
<p>Find elements by element name</p>
<pre><code contenteditable class = "html">
<ul>
<li>item 1</li>
<li>item 2</li>
<li>item 3</li>
</ul>
<a id="gdi-link" href="http://girldevelopit.com">
Girl Develop It
</a>
</code></pre>
<pre><code contenteditable class = "javascript">
$("li");
$("a");
</code></pre>
</section>
<section data-state="activity">
<h3>jQuery Selectors: element</h3>
<p><a href="class3/dom-trial.html">Try it!</a></p>
<img src="images/jquery-element.png"/>
</section>
</section>
<section>
<section>
<h3>jQuery Selectors: class</h3>
<p>Find elements by class name using <code>.</code></p>
<pre><code contenteditable class = "html">
<h1 class="header">My Page.</h1>
<div class="other">
<p>other text</p>
</div>
</code></pre>
<pre><code contenteditable class = "javascript">
$(".header");
$(".other");
</code></pre>
</section>
<section data-state="activity">
<h3>jQuery Selectors: class</h3>
<p><a href="class3/dom-trial.html">Try it!</a></p>
<img src="images/jquery-class.png"/>
</section>
</section>
<section>
<h3>jQuery Selectors</h3>
<p>jQuery supports most CSS3 selectors.</p>
<pre class="fragment"><code contenteditable class = "javascript">
$("ul li");
$(".other p");
</code></pre>
<p class="fragment">See
<a href="http://api.jquery.com/category/selectors/">the documentation</a>
for a full list</p>
</section>
<section>
<h3>jQuery Selectors: Filters</h3>
<p>Sometimes a selection has more than you need.</p>
<p><a href="http://api.jquery.com/category/traversing/filtering/">Filter methods</a>
help refine your selection.</p>
<pre class="fragment"><code contenteditable class = "javascript">
$("div").has("p");
$("li").first();
$("p").eq(2);
$("div").not("#important");
</code></pre>
</section>
<section>
<h3>saving jQuery selections</h3>
<p>If you've made a selection you might want to use again, you can save
it in a variable.</p>
<p class="fragment">This is a good idea for performance.</p>
<pre class="fragment"><code contenteditable class = "javascript">
$important = $("#important");
</code></pre>
<p class="fragment">The convention is to start the variable name with
a $ to indicate it contains a jQuery object.</p>
</section>
<!-- Actions-->
<section>
<h3>jQuery Actions</h3>
<div class = "left-align">
<p class = "fragment">jQuery has hundreds of actions that can be performed on any element</p>
<p class = "fragment">All the actions are methods</p>
<p class = "fragment">As methods they are called with dot notation</p>
</div>
<div class = "fragment">
Action format
<pre><code contenteditable class = "javascript">
$(selector).action();
</code></pre>
</div>
</section>
<section>
<section>
<h3>get and set attributes</h3>
<p>We use the <a href="http://api.jquery.com/attr/"><code>attr</code></a>
method as a <em>getter</em> and a <em>setter</em></p>
<div class="fragment">
<pre><code contenteditable class = "html">
<a id="gdi-link" href="http://girldevelopit.com">
Girl Develop It
</a>
</code></pre>
<p>Get attribute</p>
<pre><code contenteditable class = "javascript">
$("#gdi-link").attr('href');
</code></pre>
</div>
<div class="fragment">
<p>Set attribute</p>
<pre><code contenteditable class = "javascript">
var pghLink = 'http://girldevelopit.com/chapters/pittsburgh';
$("#gdi-link").attr('href', pghLink);
</code></pre>
</div>
</section>
<section data-state="activity">
<h3>get and set attributes</h3>
<p><a href="class3/dom-trial.html">Try it!</a></p>
<img src="images/jquery-attr.png"/>
</section>
</section>
<section>
<section>
<h3>get and set html</h3>
<p>We use the <a href="http://api.jquery.com/html/"><code>html</code></a>
method to <br/>get and set an element's HTML</p>
<div class="fragment">
<pre><code contenteditable class = "html">
<div id="my-data">the data</div>
</code></pre>
<p>Get html</p>
<pre><code contenteditable class = "javascript">
$("#my-data").html();
</code></pre>
</div>
<div class="fragment">
<p>Set html</p>
<pre><code contenteditable class = "javascript">
$("#my-data").html('different data!'); // Text
$("$my-data").html('<strong>Strong Data!</strong>'); // HTML
</code></pre>
</div>
</section>
<section data-state="activity">
<h3>get and set html</h3>
<p><a href="class3/dom-trial.html">Try it!</a></p>
<img src="images/jquery-html.png"/>
</section>
</section>
<section>
<section>
<h3>get and set text</h3>
<p>We use the <a href="http://api.jquery.com/text/"><code>text</code></a>
method to <br/>get and set an element's text</p>
<div class="fragment">
<pre><code contenteditable class = "html">
<div id="my-data">the data</div>
</code></pre>
<p>Get text</p>
<pre><code contenteditable class = "javascript">
$("#my-data").text();
</code></pre>
</div>
<div class="fragment">
<p>Set text</p>
<pre><code contenteditable class = "javascript">
$("#my-data").text('different data!'); // Text
</code></pre>
</div>
</section>
</section>
<section>
<section>
<h3>append and prepend</h3>
<p>We use the <a href="http://api.jquery.com/append/"><code>append</code></a>
and <a href="http://api.jquery.com/prepend/"><code>prepend</code></a>
methods to <br/>add to an element's HTML</p>
<div class="fragment">
<pre><code contenteditable class = "html">
<div id="my-data">the data</div>
</code></pre>
<p>Append - add to the end</p>
<pre><code contenteditable class = "javascript">
$("#my-data").append('back');
$("#my-data").append('<strong>strong back</strong>');
</code></pre>
</div>
<div class="fragment">
<p>Prepend - add to the beginning</p>
<pre><code contenteditable class = "javascript">
$("#my-data").prepend('front');
$("#my-data").prepend('<strong>strong front</strong>');
</code></pre>
</div>
</section>
<section data-state="activity">
<h3>append and prepend</h3>
<p><a href="class3/dom-trial.html">Try it!</a></p>
<img src="images/jquery-append-prepend.png"/>
</section>
</section>
<section>
<h3>Creating new elements</h3>
<div class = "fragment">
<pre><code contenteditable class = "javascript">
var newDiv = $('<div></div>');
</code></pre>
</div>
<div class = "fragment">
Seriously. That's it!
</div>
</section>
<section>
<h3>Removing elements</h3>
<p>Use the <a href="http://api.jquery.com/remove/"><code>remove</code></a>
method to remove the element(s).</p>
<div class = "fragment">
<pre><code contenteditable class = "html">
<div id="my-data">the data</div>
</code></pre>
<pre><code contenteditable class = "javascript">
$('#my-data').remove();
</code></pre>
</div>
</section>
<section>
<section>
<h3>get and set styles</h3>
<p>We use the <a href="http://api.jquery.com/css/"><code>css</code></a>
method as a <em>getter</em> and a <em>setter</em></p>
<div class="fragment">
<pre><code contenteditable class = "html"> <div id="styled" styles="color: red;">styled text!</div></code></pre>
<p>Get style</p>
<pre><code contenteditable class = "javascript"> $("#styled").css('color');</code></pre>
</div>
<div class="fragment">
<p>Set style</p>
<pre><code contenteditable class = "javascript">
$("#styled").css('color', 'blue');
// Set multiple styles
$("#styled").css({
'margin': '10px',
'border': 'solid 1px black'
});
</code></pre>
</div>
</section>
<section data-state="activity">
<h3>get and set styles</h3>
<p><a href="class3/dom-trial.html">Try it!</a></p>
<img src="images/jquery-css.png"/>
</section>
</section>
<section>
<h3>modify classes</h3>
<p>We use the
<a href="http://api.jquery.com/category/manipulation/class-attribute/">class manipulation</a>
methods<br/> to modify classes</p>
<div class="fragment">
<pre><code contenteditable class = "javascript">
$("#styled").addClass("active");
$("#styled").hasClass("active");
$("#styled").removeClass("active");
$("#styled").toggleClass("active");
</code></pre>
</div>
</section>
<section>
<section>
<h3>chaining</h3>
<p>Many jQuery methods return a jQuery object, <br/>so you can call
additional methods <br/>without pausing for a semicolon.</p>
<div class="fragment">
<pre><code contenteditable class = "html"> <div id="styled" styles="color: red;">styled text!</div></code></pre>
<p>Separately</p>
<pre><code contenteditable class = "javascript">
$("#styled").css('color');
$("#styled").append('extra text!');
</code></pre>
</div>
<div class="fragment">
<p>Chained</p>
<pre><code contenteditable class = "javascript">
$("#styled").css('color').append('extra text!');
</code></pre>
</div>
</section>
</section>
<section>
<section>
<h3>each</h3>
<p>jQuery's <a href="http://api.jquery.com/each/">each</a> method
makes it easy to iterate through elements.</p>
<div class="fragment">
<pre><code contenteditable class = "html">
<ul>
<li>item 1</li>
<li>item 2</li>
<li>item 3</li>
</ul>
</code></pre>
<div class="fragment">
<pre><code contenteditable class = "javascript">
$("li").each(function(index, element) {
$(element).text("This is item #" + index);
});
</code></pre>
</div>
</section>
<section data-state="activity">
<h3>each</h3>
<p><a href="class3/dom-trial.html">Try it!</a></p>
</section>
</section>
<!-- Exercise-->
<section data-state="activity">
<section>
<h3>Let's Develop It!</h3>
<h4>Convert to jQuery</h4>
<ul>
<li>Convert last week's DOM interactions into jQuery</li>
<li>Bonus: add some style changes too</li>
</ul>
</section>
<section>
<h3>Hints</h3>
<ul>
<li>Use <code>$()</code> with css selectors to find elements.</li>
<li>Look at the slides and/or the <a href="http://api.jquery.com/">jQuery API</a>
to figure out how methods work</li>
</ul>
</section>
<section>
<h3>Example</h3>
<p>Are you done or really stuck?</p>
<p>Check out <a href="class3/02-convert-jquery/index.html">our example</a>.</p>
</section>
</section>
<section>
<h3>Document Ready</h3>
<p>The DOM takes time to load</p>
<p class="left-align fragment">Immediately running JS that depends on the DOM leads to problems</p>
<div class="left-align fragment">
Document ready is a method called when the page is loaded
<pre><code contenteditable class = "javascript">
$(document).ready(function(){
// Code that depends on the DOM
});
</code></pre>
</div>
</section>
<section>
<h3>Document Ready</h3>
<div class="left-align">
It can also be written in a short way like this
<pre><code contenteditable class = "javascript">
$(function(){
// Code that depends on the DOM
});
</code></pre>
</div>
</section>
<section>
<h3>Document Ready</h3>
<div class="left-align">
<pre><code contenteditable class = "javascript">
$(document).ready(function(){
// Code that depends on the DOM
});
</code></pre>
</div>
<div class="fragment left-align"><strong>callback function</strong> -
a function that is passed as an argument, and expected to run (call back)
at an appropriate time.</div>
<br/>
<div class="fragment left-align"><strong>anonymous function</strong> -
a function without a name.</div>
</section>
<section data-state="activity">
<section>
<h3>Let's Develop It!</h3>
<h4>Document Ready</h4>
<ul>
<li>Replace <code>window.onload</code> with <code>$(document).ready</code></li>
</ul>
</section>
<section>
<h3>Example</h3>
<p>Are you done or really stuck?</p>
<p>Check out <a href="class3/03-document-ready/index.html">our example</a>.</p>
</section>
</section>
<!-- HTML Events-->
<section>
<h3>HTML events</h3>
<p>Events occur on a webpage via user interaction</p>
<p class = "fragment">Common Events:</p>
<ul>
<li class="fragment">
<a href="http://api.jquery.com/click/"><code>click</code></a> -
mouse clicks an element
</li>
<li class= "fragment">
<a href="http://api.jquery.com/mouseenter/"><code>mouseenter</code></a> -
mouse goes inside an element
</li>
<li class = "fragment">
<a href="http://api.jquery.com/mouseleave/"><code>mouseleave</code></a> -
mouse leaves an element
</li>
<li class = "fragment"><a href = "http://api.jquery.com/category/events/" target = "_blank">Other events</a></li>
</ul>
</section>
<section>
<h3>Handling events</h3>
<pre><code contenteditable class = "javascript">
$(selector).mouseenter(function(){
//code when the mouse enters
});
</code></pre>
<div class = "fragment">
<pre><code contenteditable class = "javascript">
$('.box').mouseenter(function(){
$(this).css('background-color', 'purple')
});
</code></pre>
</div>
<div class = "fragment left-align">
<div>The <span class = "green">$(this)</span> selector in jQuery refers to the element on whom the action was called.</div>
<div>Here <span class = "green">$(this)</span> is the <span class = "blue">$('.box')</span> that the mouse entered.
</div>
</section>
<section>
<h3>Handling event examples</h3>
<div class = "fragment">
<pre><code contenteditable class = "javascript">
$('.box').mouseenter(function(){
$(this).css('background-color', 'purple')
});
</code></pre>
</div>
<div class = "fragment">
<pre><code contenteditable class = "javascript">
$('.box').mouseleave(function(){
$(this).css('background-color', 'orange')
});
</code></pre>
</div>
<div class = "fragment">
<pre><code contenteditable class = "javascript">
$('.box').click(function(){
$(this).css('background-color', 'green')
});
</code></pre>
</div>
</section>
<section>
<h3>Combining events</h3>
<p>If you want multiple events to happen on the same element, you
should use the <a href="http://api.jquery.com/on/"><code>on</code></a> method</p>
<div class = "fragment">
<pre><code contenteditable class = "javascript">
$('.box').on({
click: function() {
$(this).css('background-color', 'green')
},
mouseenter: function() {
$(this).css('background-color', 'purple')
},
mouseleave: function(){
$(this).css('background-color', 'orange')
}
});
</code></pre>
</div>
<a href="class3/event-examples.html" class="fragment">Try it!</a>
</section>
<!-- Exercise-->
<section data-state="activity">
<section>
<h3>Let's Develop It!</h3>
<h4>Events</h4>
<ul>
<li>Add a div to your html</li>
<li>Bind events to your div in the javascript file</li>
<li>Change the div when events trigger</li>
<li>Bonus: change all the onclick events to jQuery click events</li>
</ul>
</section>
<section>
<h3>Hints</h3>
<ul>
<li>Remember to put things inside <code>document.ready</code></li>
<li>Some suggested things to change on event: color, text, size</li>
</ul>
</section>
<section>
<h3>Example</h3>
<p>Are you done or really stuck?</p>
<p>Check out <a href="class3/04-events/index.html">our example</a>.</p>
</section>
</section>
<!-- HTML forms-->
<section>
<h3>HTML forms</h3>
<p>HTML Forms allow users to enter information</p>
<div class = "fragment">
<pre><code contenteditable class = "html">
<form id="about-me">
<input type="text" id="name" placeholder="Enter a name"/>
<label>Do you like popcorn</label>
Yes <input type= "radio" name="popcorn" val="yes"/>
No <input type= "radio" name="popcorn" val="no"/>
<label>Favorite Dinosaur</label>
<select id="dinosaur">
<option value="t-rex">Tyrannosaurus Rex</option>
<option value="tri">Triceratops</option>
<option value="stego">Stegosaurus</option>
<option value="other">Other</option>
</select>
<input type="submit" value="Go!" style="padding: 7px; font-size:1em;"/>
</form>
</code></pre>
</div>
</section>
<section>
<h3>HTML forms</h3>
<p>HTML Forms allow users to enter information</p>
<div class = "fragment left-align">
<form>
<input type = "text" id = "name" placeholder = "Enter a name" style = "width:300px; padding: 7px; font-size:1em"/><br/>
<label>Do you like popcorn</label><br/>
Yes <input type = "radio" name = "popcorn" val = "yes" style ="font-size:1em"/>
No <input type = "radio" name = "popcorn" val = "no" style ="font-size:1em"/><br/>
<label>Favorite Dinosaur</label><br/>
<select id = "dinosaur" style = "padding: 7px; font-size:1em">
<option>Tyrannosaurus Rex</option>
<option>Triceratops</option>
<option>Stegosaurus</option>
<option>Other</option>
</select><br/>
<input type = "submit" value = "Go!" style = "padding: 7px; font-size:1em"/>
</form>
</div>
</section>
<!-- JavaScript and HTML Forms-->
<section>
<h3>Values from Forms</h3>
<p>You can use <a href="http://api.jquery.com/val/">val</a> to get values from a form</p>
<div class = "fragment">
<pre><code contenteditable class = "javascript">
$('#name').val();
$('select#dinosaur').val();
$('input:radio[name=popcorn]:checked').val();
</code></pre>
</div>
<div class = "fragment">
Or set values of a form
<pre><code contenteditable class = "javascript">
$('#name').val('Mitch');
$('select#dinosaur').val('stego');
$('input:radio[name=popcorn]:checked').val('no');
</code></pre>
</div>
</section>
<section>
<h3>Values from Forms</h3>
<p>jQuery has an event for form submission</p>
<div class = "fragment">
<pre><code contenteditable class = "javascript">
$('#about-me').submit(function(event){
//code to execute on submission
return false;
});
</code></pre>
<span class = "yellow">"return false"</span> to prevent the form trying
<br/> to submit to a server.
</div>
</section>
<section data-state="activity">
<section>
<h3>Let's Develop It!</h3>
<h4>Forms</h4>
<ul>
<li>Start with your favorite activity calculator</li>
<li>Create a form to send dynamic data to the function when you click the button</li>
<li>Bonus: check the form input and alert the user if it is invalid</li>
</ul>
</section>
<section>
<h3>Hints</h3>
<ul>
<li>Put a text input in the form to get the name of the activity</li>
<li>Put a text input in the form to get the times a month you do
the activity</li>
<li>Use <code>return false;</code> to prevent the form from
submitting normally</li>
</ul>
</section>
<section>
<h3>Hints</h3>
<p>Here's an example form.</p>
<pre><code contenteditable class="html">
<form id="activitiesForm">
<label for="activityName">Activity:</label>
<input type="text" id="activityName" />
<label for="timesPerMonth">Times a month:</label>
<input type="text" id="timesPerMonth" />
<input type="submit" value="Calculate" />
</form>
</code></pre>
</section>
<section>
<h3>Example</h3>
<p>Are you done or really stuck?</p>
<p>Check out <a href="class3/05-forms/index.html">our example</a>.</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>
<script src="reveal/js/reveal.min.js"></script>
<script>
// Full list of configuration options available here:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: true,
history: true,
theme: Reveal.getQueryHash().theme || 'gdipagano', // available themes are in /css/theme
transition: Reveal.getQueryHash().transition || 'linear', // default/cube/page/concave/zoom/linear/none
// Optional libraries used to extend on reveal.js
dependencies: [
{ src: 'reveal/lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'reveal/plugin/markdown/showdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'reveal/plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'reveal/plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: 'reveal/plugin/zoom-js/zoom.js', async: true, condition: function() { return !!document.body.classList; } },
{ src: 'reveal/plugin/notes/notes.js', async: true, condition: function() { return !!document.body.classList; } }
]
});
</script>
</body>
</html>