jQuery Jay Urbain, Ph.D. Credits: h8p://jquery.com/ - - PowerPoint PPT Presentation

jquery
SMART_READER_LITE
LIVE PREVIEW

jQuery Jay Urbain, Ph.D. Credits: h8p://jquery.com/ - - PowerPoint PPT Presentation

jQuery Jay Urbain, Ph.D. Credits: h8p://jquery.com/ h8p://api.jquery.com/ h8p://jquery.bassistance.de/jquery-geAng-started.html, Jrn Zaefferer


slide-1
SLIDE 1

1

jQuery ¡

¡ Jay ¡Urbain, ¡Ph.D. ¡

Credits: ¡ h8p://jquery.com/ ¡ h8p://api.jquery.com/ ¡ h8p://jquery.bassistance.de/jquery-­‑geAng-­‑started.html, ¡Jörn ¡Zaefferer ¡ h8p://www.mkyong.com/jquery/jquery-­‑form-­‑selectors-­‑example/ ¡ Chaffer, ¡Jonathan ¡(2011-­‑09-­‑23). ¡Learning ¡jQuery, ¡Third ¡EdiTon ¡ http://www.w3schools.com ¡ ¡

slide-2
SLIDE 2

How ¡do ¡you ¡comfort ¡a ¡JavaScript ¡bug? ¡ ¡

2

slide-3
SLIDE 3

How ¡do ¡you ¡comfort ¡a ¡JavaScript ¡bug? ¡ ¡ You ¡console ¡it. ¡

3

slide-4
SLIDE 4

What ¡is ¡jQuery? ¡

jQuery: ¡General-­‑purpose ¡abstracTon ¡layer ¡for ¡common ¡web ¡

  • scripTng. ¡
  • JavaScript ¡Library. ¡
  • Simplifies ¡JavaScript ¡programming. ¡
  • Easy ¡to ¡learn ¡– ¡once ¡you ¡have ¡HTML/DOM, ¡CSS, ¡& ¡JS ¡knowledge. ¡
  • AlternaTves: ¡Angular, ¡React ¡– ¡not ¡mutually ¡exclusive, ¡more ¡
  • later. ¡

4

slide-5
SLIDE 5

Why ¡use ¡jQuery? ¡

  • jQuery ¡makes ¡it ¡easier ¡to ¡use ¡JavaScript ¡on ¡your ¡website: ¡

– HTML/DOM ¡manipulaTon ¡ – CSS ¡manipulaTon ¡ – HTML ¡event ¡handling ¡ – Effects ¡and ¡animaTons ¡ – AJAX ¡ ¡ – UTliTes ¡ ¡

  • Also ¡provides: ¡

– Enhancements ¡to ¡basic ¡JavaScript ¡constructs. ¡ – Browser/pla7orm ¡independence! ¡

5

slide-6
SLIDE 6

Why ¡does ¡jQuery ¡work ¡well? ¡

  • Open ¡source. ¡API: ¡h8p://api.jquery.com ¡
  • Maintains ¡a ¡wide ¡range ¡of ¡features, ¡relaTvely ¡compact ¡(~87kB). ¡
  • Leverages ¡user’s ¡knowledge ¡of ¡CSS ¡selectors. ¡
  • Supports ¡extensions ¡ ¡(plugins) ¡-­‑ ¡avoids ¡feature ¡creep. ¡
  • Works ¡with ¡sets ¡– ¡implied ¡itera0on. ¡ ¡
  • Allows ¡mulTple ¡acTons ¡in ¡one ¡line ¡– ¡chaining ¡ ¡a().b().c() ¡ ¡

6

slide-7
SLIDE 7

Using ¡jQuery ¡

1. Download ¡a ¡copy ¡of ¡jQuery: ¡h8p://jquery.com/download/ ¡ – Compressed ¡and ¡uncompressed ¡copies ¡are ¡available. ¡ ¡ – Uncompressed ¡file ¡is ¡best ¡used ¡during ¡development ¡or ¡debugging. ¡

  • Also ¡great ¡code ¡examples ¡for ¡learning ¡JavaScript. ¡

– The ¡compressed ¡file ¡saves ¡bandwidth ¡and ¡improves ¡performance ¡in ¡

  • producTon. ¡ ¡
  • You ¡can ¡also ¡download ¡a ¡sourcemap ¡file ¡for ¡use ¡when ¡debugging ¡with ¡a ¡

compressed ¡file. ¡Not ¡required ¡for ¡running ¡jQuery. ¡ – You ¡can ¡also ¡access ¡via ¡a ¡CDN. ¡Here’s ¡Google’s, ¡there ¡are ¡many: ¡ – <script ¡src="h8ps://ajax.googleapis.com/ajax/libs/jquery/3.2.1/ jquery.min.js"></script> ¡ – jquery-­‑3.2.1.min.js ¡– ¡compressed ¡ – jquery-­‑3.2.1.js ¡– ¡release ¡version ¡(can ¡read ¡this) ¡ 2. Add ¡reference: ¡<script ¡src=“jquery-­‑3.2.1.js"></script> ¡ – Customary ¡to ¡use ¡<script ¡src=“jquery.js"></script> ¡and ¡relabel ¡w/ ¡new ¡

  • versions. ¡

7

Note: show jquery-3.2.1.js

slide-8
SLIDE 8

Referencing ¡jQuery ¡(~10k ¡lines) ¡at ¡the ¡end ¡of ¡your ¡html ¡ document ¡allows ¡the ¡rest ¡of ¡your ¡page ¡to ¡load ¡a ¡li8le ¡faster ¡

<!DOCTYPE html> <html> <head> <title>Your title</title> </head> <body> <!-- Your html goes here --> <!-- Finally, include jQuery, then your own .js files and code --> <script src="jquery-3.2.1.js"></script> <script src="myJavascript.js"></script> <script> // your remaining code goes here </script> </body> </html>

8

slide-9
SLIDE 9

jQuery ¡philosophy ¡

  • Focus ¡on ¡the ¡interacTon ¡between ¡JavaScript ¡and ¡HTML/DOM ¡

¡

  • (Almost) ¡every ¡operaTon ¡boils ¡down ¡to: ¡

– Find ¡DOM ¡element ¡ – Do ¡something ¡to ¡it ¡

9

slide-10
SLIDE 10

jQuery ¡Syntax ¡

Basic ¡syntax ¡is: ¡$(selector).ac)on() ¡

  • A ¡$ ¡is ¡reference ¡to ¡jQuery ¡
  • A ¡(selector) ¡to ¡"query ¡(or ¡find)" ¡HTML ¡elements. ¡
  • A ¡jQuery ¡ac)on() ¡to ¡be ¡performed ¡on ¡the ¡element(s) ¡

Examples: ¡

  • $(this).hide() ¡-­‑ ¡hides ¡the ¡current ¡element. ¡
  • $("p").hide() ¡-­‑ ¡hides ¡all ¡<p> ¡elements. ¡
  • $(".test").hide() ¡-­‑ ¡hides ¡all ¡elements ¡with ¡class="test". ¡
  • $("#test").hide() ¡-­‑ ¡hides ¡the ¡element ¡with ¡id="test". ¡

10

slide-11
SLIDE 11

Regular ¡JavaScript ¡(no ¡jQuery) ¡

11

jq1.html

  • Typical ¡to ¡wrap ¡JavaScript ¡in ¡an ¡onload ¡funcTon ¡to ¡ensure ¡that ¡JS ¡

code ¡runs ¡a,er ¡the ¡browser ¡finishes ¡loading ¡the ¡document. ¡

slide-12
SLIDE 12

Hello ¡jQuery ¡

12

hellojQuery.html Load jQuery lib Add events as soon as the document element Is ready jQuery alternative ready syntax: $(function(){ // jQuery methods go here... }); E.g.: $(alert("hello jQuery"));

slide-13
SLIDE 13

$(document).ready ¡vs ¡$(window).load ¡

  • $(window).load ¡executes ¡aser ¡your ¡pages ¡loads, ¡including ¡any ¡

images ¡your ¡page ¡may ¡display. ¡ ¡

  • Browsers ¡load ¡images ¡on ¡separate ¡threads, ¡since ¡image ¡

loading ¡can ¡take ¡a ¡long ¡Tme. ¡

  • $(document).ready ¡executes ¡as ¡soon ¡as ¡the ¡browser ¡

completes ¡DOM ¡creaTon, ¡which ¡occurs ¡(typically) ¡before ¡all ¡of ¡ the ¡images ¡are ¡loaded. ¡

  • Your ¡jQuery ¡script ¡can ¡thus ¡start ¡execuTng ¡more ¡quickly ¡aser ¡

a ¡user ¡browses ¡to ¡your ¡page. ¡ ¡

13

slide-14
SLIDE 14

What ¡is ¡that ¡$ ¡about? ¡

Almost ¡everything ¡starts ¡with ¡a ¡call ¡to ¡the ¡jQuery() ¡selector ¡funcTon. ¡

  • Since ¡it’s ¡called ¡so ¡osen, ¡the ¡$ ¡global ¡variable ¡is ¡defined ¡as ¡an ¡

alias ¡to ¡the ¡funcTon: ¡ //alias to jQuery function window.jQuery = window.$ = jQuery;

  • Thus ¡you ¡could ¡write ¡either: ¡

$(“#p2”).html = “hello se2840”; Or jQuery(“#p2”).html = “hello se2840”;

14

slide-15
SLIDE 15

Method ¡chaining ¡

$("h1").fadeOut().fadeIn();

fadeIn() ¡executes ¡aser ¡fadeOut() ¡completes. ¡ ¡ fadeOut() ¡and ¡fadeIn() ¡are ¡placed ¡in ¡jQuery’s ¡animaTon ¡queue, ¡ which ¡processes ¡acTons ¡in ¡sequence. ¡ ¡ ¡ ¡

15

slide-16
SLIDE 16

$( ¡selector ¡) ¡

selector ¡can ¡be ¡one ¡of ¡the ¡following: ¡ – A ¡CSS ¡selector ¡expression ¡ ¡ – A ¡string ¡of ¡HTML ¡ ¡ – A ¡JavaScript ¡object ¡

16

slide-17
SLIDE 17

Element ¡selector ¡

<!DOCTYPE ¡html> ¡

<html> ¡ <head> ¡ ¡ ¡ ¡ ¡<script ¡src="jquery-­‑3.1.1.js”></script> ¡ ¡ ¡ ¡ ¡<script> ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡$(document).ready(funcTon(){ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡$("#hide").click(funcTon(){ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡$("p").hide(); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡}); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡$("#show").click(funcTon(){ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡$("p").show(); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡}); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡}); ¡ ¡ ¡ ¡ ¡</script> ¡ </head> ¡ <body> ¡ <p>If ¡you ¡click ¡on ¡the ¡"Hide" ¡bu8on, ¡I ¡will ¡disappear.</p> ¡ <bu8on ¡id="hide">Hide</bu8on> ¡ <bu8on ¡id="show">Show</bu8on> ¡ </body> ¡ </html> ¡

17

jQuerySelectorHide.html Anonymous functions Add onclick event handler $(“p”) – select all <p> elements on a page:

slide-18
SLIDE 18

#id ¡Selector ¡

  • The ¡jQuery ¡#id ¡selector ¡uses ¡the ¡id ¡a8ribute ¡of ¡an ¡HTML ¡tag ¡to ¡find ¡

the ¡specific ¡element. ¡

¡ <script> ¡ $(document).ready(funcTon(){ ¡ ¡ ¡$("bu8on").click(funcTon(){ ¡ ¡ ¡ ¡ ¡$("#test").hide(); ¡ ¡ ¡}); ¡ }); ¡ </script> ¡ ¡ <body> ¡ <h2>This ¡is ¡a ¡heading</h2> ¡ <p>This ¡is ¡a ¡paragraph.</p> ¡ <p ¡id="test">This ¡is ¡another ¡paragraph.</p> ¡ <bu8on>Click ¡me</bu8on> ¡ </body> ¡

18

slide-19
SLIDE 19

.class ¡Selector ¡

  • The ¡jQuery ¡class ¡selector ¡finds ¡elements ¡with ¡a ¡specific ¡class. ¡

<script> ¡ $(document).ready(funcTon(){ ¡ ¡ ¡$("bu8on").click(funcTon(){ ¡ ¡ ¡ ¡ ¡$(".test").hide(); ¡ ¡ ¡}); ¡ }); ¡ </script> ¡ ¡ <body> ¡ <h2 ¡class="test">This ¡is ¡a ¡heading</h2> ¡ <p ¡class="test">This ¡is ¡a ¡paragraph.</p> ¡ <p>This ¡is ¡another ¡paragraph.</p> ¡ <bu8on>Click ¡me</bu8on> ¡ </body> ¡

19

slide-20
SLIDE 20

Examples ¡of ¡jQuery ¡Selectors ¡

20

slide-21
SLIDE 21

jQuery ¡Event ¡Methods ¡

  • User ¡acTons ¡that ¡a ¡web ¡page ¡can ¡respond ¡to ¡are ¡called ¡events: ¡

– moving ¡a ¡mouse ¡over ¡an ¡element ¡ – selecTng ¡a ¡radio ¡bu8on ¡ – clicking ¡on ¡an ¡element ¡

21

slide-22
SLIDE 22

jQuery ¡Syntax ¡For ¡Event ¡Methods ¡

¡<script> ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡$(document).ready(funcTon(){ ¡ ¡// ¡ready ¡event ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡$("bu8on").click(funcTon(){ ¡ ¡ ¡// ¡click ¡event ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡$("p").hide(); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡}); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡$("#p1").mouseenter(funcTon(){// ¡mouse ¡enter ¡event ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡alert("You ¡entered ¡p1!"); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡}); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡$("#p1").mouseup(funcTon(){// ¡mouse ¡up ¡event ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡alert("Mouse ¡up ¡over ¡p1!"); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡}); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡$("#p1").hover(funcTon(){// ¡hover ¡event ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡alert("You ¡entered ¡p1!"); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡}, ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡funcTon(){ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡alert("Bye! ¡You ¡now ¡leave ¡p1!"); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡}); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡}); ¡ ¡ ¡ ¡ ¡</script> ¡

22

Useful events:

  • click()
  • dblclick()
  • mouseenter()
  • mouseleave()
  • mousedown()
  • mouseup()
  • hover()
slide-23
SLIDE 23

jQuery ¡Effects: ¡hide() ¡and ¡show() ¡ ¡

Syntax: ¡

  • $(selector).hide(speed, ¡callback); ¡
  • $(selector).show(speed, ¡callback); ¡

¡ $("#hide").click(funcTon(){ ¡ ¡ ¡$("p").hide(); ¡ }); ¡ ¡ $("#show").click(funcTon(){ ¡ ¡ ¡$("p").show(1000); ¡ }); ¡

23

jQueryHideAndShow.html – Note: modify times // callback at end, needs to be in function

slide-24
SLIDE 24

jQuery ¡Effects: ¡toggle ¡

Syntax: ¡toggle ¡between ¡the ¡hide() ¡ and ¡show() ¡ ¡

  • $(selector).toggle(speed,callback); ¡

$("bu8on").click(funcTon(){ ¡ ¡ ¡$("p").toggle(); ¡ }); ¡ ¡ ¡

24

<!DOCTYPE html> <html> <head> <script src="jquery.js"> </script> <script> $(document).ready(function(){ $("button").click(function(){ $("p").toggle(); }); }); </script> </head> <body> <p>This is a paragraph.</p> <button>Toggle between hide() and show()</button> </body> </html>

slide-25
SLIDE 25

jQuery ¡Effect ¡Methods ¡Reference: ¡ h8p://www.w3schools.com/jquery/jquery_ref_effects.asp ¡

¡

25

slide-26
SLIDE 26

jQuery ¡Effects: ¡fade ¡

  • fadeIn() ¡
  • fadeOut() ¡
  • fadeToggle() ¡
  • fadeTo() ¡

¡ Syntax: ¡

  • $(selector).fadeIn(speed,callback); ¡ ¡// ¡speed ¡{“slow”, ¡“fast”, ¡or ¡millisec’s} ¡

$("bu8on").click(funcTon(){ ¡ ¡ ¡$("#div1").fadeIn(); ¡ ¡ ¡$("#div2").fadeIn("slow"); ¡ ¡ ¡ ¡$("#div3").fadeIn(3000); ¡ }); ¡ ¡ ¡

26

jQueryFade.html

slide-27
SLIDE 27

jQuery ¡Effects: ¡slide ¡

  • slideDown() ¡
  • slideUp() ¡
  • slideToggle() ¡

¡ Syntax: ¡

  • $(selector).slideDown(speed,callback); ¡

¡ $("#flip").click(funcTon(){ ¡ ¡ ¡$("#panel").slideDown(); ¡ }); ¡ ¡

27

slide-28
SLIDE 28

jQuery ¡Effects: ¡animate ¡

Create ¡custom ¡animaOons ¡ ¡ Syntax: ¡toggle ¡between ¡the ¡hide() ¡and ¡show() ¡ ¡

  • $(selector).animate({params},speed,callback); ¡

¡<script> ¡

¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡$(document).ready(funcTon(){ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡$("bu8on").click(funcTon(){ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡$("div").animate({ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡les:'250px’, ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡opacity:'0.5’, ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡height:'150px’, ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡width:'150px’ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡}, ¡5000, ¡funcTon() ¡{alert("done")} ¡); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡}); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡}); ¡ ¡ ¡ ¡ ¡</script> ¡

28

jQueryAnimate.html

Using relative values: var $square = $("#square"); function bounce() { $square.animate({ top: "+=10" }, 300, function() { $square.animate({ top: "-=10" }, 300, function() { bounce(); }) }); } $(document).ready(function(){ bounce(); });

Also: continuousAnimationJQ.html

slide-29
SLIDE 29

jQuery ¡Effects: ¡stop ¡

Stop ¡an ¡animaTon ¡or ¡effect ¡before ¡it ¡is ¡finished ¡ Stop ¡syntax: ¡ ¡

$(selector).stop(stopAll,goToEnd); ¡ ¡ // ¡gotToEnd ¡true/false ¡complete ¡animaTon ¡or ¡stop. ¡Default ¡is ¡false. ¡ Chaining: ¡browsers ¡do ¡not ¡have ¡to ¡find ¡the ¡same ¡element(s) ¡more ¡than ¡once. ¡

  • $("#p1").css("color","red").slideUp(2000).slideDown(2000); ¡

¡

29

slide-30
SLIDE 30

jQuery ¡Effects: ¡callback ¡

  • JavaScript ¡statements ¡are ¡executed ¡line ¡by ¡line. ¡ ¡
  • With ¡effects, ¡the ¡next ¡line ¡of ¡code ¡can ¡be ¡run ¡even ¡though ¡the ¡

effect ¡is ¡not ¡finished. ¡ ¡

  • This ¡can ¡create ¡errors. ¡
  • To ¡prevent ¡this, ¡you ¡can ¡create ¡a ¡callback ¡funcTon. ¡
  • A ¡callback ¡funcTon ¡is ¡executed ¡aser ¡the ¡current ¡effect ¡is ¡finished. ¡

Typical ¡syntax: ¡$(selector).hide(speed,callback); ¡ ¡ $("bu8on").click(funcTon(){ ¡ ¡ ¡$("p").hide("slow",funcTon(){ ¡ ¡ ¡ ¡ ¡alert("The ¡paragraph ¡is ¡now ¡hidden"); ¡ ¡ ¡}); ¡ }); ¡

30

slide-31
SLIDE 31

Following ¡examples: ¡jquery.zip ¡

  • Download ¡jquery.zip ¡from ¡course ¡outline ¡

31

slide-32
SLIDE 32

Form ¡Selector ¡Examples ¡

  • Form ¡selectors ¡can ¡be ¡used ¡individually ¡or ¡combined ¡for ¡

greater ¡specificity. ¡

  • Reference ¡– ¡jQueryForm.html ¡

32

slide-33
SLIDE 33

Form ¡Selectors ¡

33

slide-34
SLIDE 34

Text ¡– ¡$(‘input:text’) ¡

To ¡select ¡text ¡ $('input:text'); ¡ ¡

  • To ¡get ¡the ¡textbox ¡value ¡

$('input:text').val(); ¡ ¡ ¡

  • To ¡set ¡the ¡textbox ¡value ¡

$('input:text').val("New ¡Text"); ¡ ¡ <input ¡type="text" ¡value="This ¡is ¡message ¡from ¡textbox"> ¡ ¡

34

slide-35
SLIDE 35

Password ¡– ¡$(‘input:password’) ¡

To ¡select ¡password ¡ $('input:password'); ¡

  • To ¡get ¡the ¡password ¡value ¡

$('input:password').val(); ¡

  • To ¡set ¡the ¡password ¡value ¡

$('input:password').val("New ¡Text"); ¡ ¡ <input ¡type="password" ¡value="This ¡is ¡message ¡from ¡password"> ¡

35

slide-36
SLIDE 36

Textarea ¡– ¡$(‘input:textarea’) ¡

To ¡select ¡textarea ¡ $('textarea'); ¡

  • To ¡get ¡the ¡textarea ¡value ¡

$('textarea').val(); ¡

  • To ¡set ¡the ¡textarea ¡value ¡

$('textarea').val("New ¡Text ¡in ¡Textarea"); ¡ ¡ <textarea>This ¡is ¡message ¡from ¡textarea</textarea> ¡

36

slide-37
SLIDE 37

Radio ¡BuTons– ¡$(‘input:radio’) ¡

To ¡select ¡radio ¡buTon ¡ $('input:radio'); ¡

  • To ¡get ¡the ¡selected ¡radio ¡bu8on ¡opTon ¡

$('input:radio[name=radiobu8on-­‑name]:checked').val(); ¡

  • To ¡select ¡the ¡first ¡radio ¡bu8on ¡opTon ¡

$('input:radio[name=radiobu8on-­‑name]:nth(0)').a8r('checked',true); ¡ ¡

  • or ¡ ¡

$('input:radio[name=radiobu8on-­‑name]')[0].checked ¡= ¡true; ¡ ¡ <input ¡name="sex" ¡type="radio" ¡value="Male" ¡checked>Male</input> ¡ <input ¡name="sex" ¡type="radio" ¡value="Female">Female</input> ¡

37

slide-38
SLIDE 38

Checkbox– ¡$(‘input:checkbox’) ¡

To ¡select ¡checkbox ¡ $('input:checkbox'); ¡

  • To ¡check ¡whether ¡a ¡checkbox ¡is ¡checked ¡

$('input:checkbox[name=checkboxname]').is(':checked'); ¡

  • To ¡check ¡a ¡checkbox ¡

$('input:checkbox[name=checkboxname]').a8r('checked',true); ¡

  • To ¡unchecked ¡a ¡checkbox ¡

$('input:checkbox[name=checkboxname]').a8r('checked',false); ¡ ¡ <input ¡type="checkbox" ¡name="checkme">Check ¡Me</input> ¡

38

slide-39
SLIDE 39

File ¡– ¡$(‘input:file’) ¡

To ¡select ¡a ¡file ¡ $('input:file'); ¡

  • To ¡get ¡the ¡file ¡value ¡

$('input:file').val(); ¡ ¡ <input ¡type="file"> ¡

39

slide-40
SLIDE 40

Hidden ¡value– ¡$(‘input:hidden’) ¡

To ¡select ¡a ¡hidden ¡value ¡ $('input:hidden'); ¡

  • To ¡get ¡the ¡hidden ¡value ¡

$('input:hidden').val(); ¡

  • To ¡set ¡the ¡hidden ¡value ¡

$('input:hidden').val("New ¡Text"); ¡ ¡ <input ¡type="hidden" ¡value="This ¡is ¡message ¡from ¡hidden"/> ¡

40

slide-41
SLIDE 41

Select ¡– ¡$(‘select’) ¡

  • To ¡select ¡– ¡select ¡box ¡

$('select') ¡or ¡$('#dropdown-­‑id') ¡

  • To ¡get ¡the ¡selected ¡drop ¡down ¡box ¡value ¡

$('#dropdown-­‑id').val(); ¡

  • To ¡set ¡the ¡drop ¡down ¡box ¡value ¡to ¡“China” ¡

$("#dropdown-­‑id").val("China"); ¡

41

slide-42
SLIDE 42

Submit– ¡$(‘input.submit’) ¡

To ¡select ¡a ¡submit ¡buTon ¡ $('input:submit'); ¡ Reset ¡buTon ¡– ¡$(‘input:reset’) ¡ To ¡select ¡a ¡reset ¡buTon ¡ $('input:reset'); ¡

42

slide-43
SLIDE 43

Demo ¡– ¡jQueryForm.html ¡

43

slide-44
SLIDE 44

Ajax ¡

  • Ability ¡to ¡manage ¡Ajax ¡calls/callbacks ¡– ¡later. ¡

44

slide-45
SLIDE 45

STOP ¡

  • AddiTonal ¡examples ¡follow ¡

45

slide-46
SLIDE 46

CSS ¡Selectors ¡ Credits: ¡Jörn ¡Zaefferer ¡

  • 1. Download ¡starterkit.zip ¡from ¡the ¡course ¡outline ¡
  • 2. Unzip ¡to ¡a ¡convenient ¡directory ¡
  • 3. Add ¡jquery.js ¡to ¡your ¡directory ¡
  • 4. Should ¡see ¡something ¡like ¡this ¡when ¡you ¡browse ¡

starterkit.html: ¡

46

slide-47
SLIDE 47

CSS ¡Selector ¡Example ¡1: ¡ ¡ Select ¡and ¡modify ¡the ¡first ¡ordered ¡list ¡in ¡starterkit.html ¡

  • TradiTonal ¡JS: ¡document.getElementById("orderedlist") ¡
  • jQuery: ¡$("#orderedlist") ¡ ¡
  • Add ¡the ¡following ¡to ¡custom.js ¡

¡$(document).ready(funcTon() ¡{ ¡ ¡ ¡ ¡ ¡ ¡$("#orderedlist").addClass("red"); ¡ ¡ ¡}); ¡ ¡

47

screen.css starterkit.html

slide-48
SLIDE 48

CSS ¡Selector ¡Output ¡

  • Should ¡see ¡something ¡like ¡the ¡following: ¡

48

slide-49
SLIDE 49

CSS ¡Selectors ¡Example ¡2 ¡

  • Add ¡more ¡classes ¡to ¡the ¡child ¡elements ¡of ¡this ¡list. ¡ ¡
  • Add ¡the ¡following ¡to ¡custom.js ¡

$(document).ready(funcTon() ¡{ ¡ ¡ ¡$("#orderedlist ¡> ¡li").addClass("blue"); ¡ ¡ }); ¡ ¡

  • This ¡selects ¡all ¡child ¡li ¡elements ¡of ¡the ¡elements ¡with ¡the ¡id ¡
  • rderedlist ¡and ¡adds ¡the ¡class ¡"blue". ¡

49

slide-50
SLIDE 50

Using ¡CSS ¡Selectors ¡for ¡Special ¡Effects ¡

  • Add ¡and ¡remove ¡the ¡class ¡when ¡the ¡user ¡hovers ¡the ¡li ¡

element, ¡but ¡only ¡on ¡the ¡last ¡element ¡in ¡the ¡list. ¡ ¡

  • Add ¡the ¡following ¡to ¡custom.js ¡

$(document).ready(funcTon() ¡{ ¡ ¡ ¡$("#orderedlist ¡li:last").hover(funcTon() ¡{ ¡ ¡ ¡ ¡ ¡$(this).addClass("green"); ¡ ¡ ¡},funcTon(){ ¡ ¡ ¡ ¡$(this).removeClass("green"); ¡ ¡ ¡}); ¡ ¡ }); ¡

50

  • 3. Third element,
slide-51
SLIDE 51

find() ¡and ¡each() ¡

  • find() ¡allows ¡you ¡to ¡search ¡the ¡descendants ¡of ¡the ¡already ¡

selected ¡elements. ¡ ¡

  • $("#orderedlist").find("li") ¡is ¡about ¡the ¡same ¡as ¡$

("#orderedlist ¡li"). ¡

  • each() ¡iterates ¡over ¡every ¡element ¡and ¡allows ¡further ¡
  • processing. ¡ ¡
  • Most ¡methods, ¡like ¡addClass(), ¡use ¡each() ¡themselves. ¡
  • Add ¡the ¡following ¡to ¡custom.js ¡

$(document).ready(funcTon() ¡{ ¡ ¡ ¡ ¡$("#orderedlist").find("li").each(funcTon(i) ¡{ ¡ ¡ ¡ ¡ ¡ ¡$(this).append( ¡" ¡BAM! ¡" ¡+ ¡i ¡); ¡ ¡ ¡ ¡}); ¡ ¡}); ¡

51

slide-52
SLIDE 52

Using ¡Effects ¡

  • Simple ¡animaTons ¡with ¡jQuery ¡can ¡be ¡achieved ¡with ¡show() ¡

and ¡hide(). ¡ ¡

  • Add ¡to ¡custom.js ¡and ¡refresh ¡browser. ¡

$(document).ready(funcTon(){ ¡ ¡ ¡ ¡ ¡ ¡$("bu8on").toggle(funcTon(){ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡$(".stuff").hide('slow'); ¡ ¡ ¡ ¡ ¡ ¡ ¡},funcTon(){ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡$(".stuff").show('fast'); ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡); ¡ ¡}); ¡

52

slide-53
SLIDE 53

AddiTonal ¡References ¡

jQuery ¡Base ¡

  • h8p://api.jquery.com/category/core/?rdfrom=h8p%3A%2F%2Fdocs.jquery.com

%2Fmw%2Findex.php%3FTtle%3DCore%26redirect%3Dno ¡ jQuery ¡Selectors ¡

  • h8p://api.jquery.com/category/selectors/?rdfrom=h8p%3A%2F

%2Fdocs.jquery.com%2Fmw%2Findex.php%3FTtle%3DSelectors%26redirect %3Dno ¡ ¡ jQuery ¡Events ¡

  • h8p://api.jquery.com/category/events/?rdfrom=h8p%3A%2F%2Fdocs.jquery.com

%2Fmw%2Findex.php%3FTtle%3DEvents%26redirect%3Dno ¡

  • Many ¡examples ¡of ¡using ¡selectors: ¡

h8p://docs.jquery.com/DOM/Traversing/Selectors ¡

53