Slide 1

Slide 1 text

0 TO HERO SEPTEMBER 2011 A jQuery Primer

Slide 2

Slide 2 text

WHAT JavaScript framework Cross-browser compatible Lightweight (31KB) APIs for DOM, events, animation, Ajax

Slide 3

Slide 3 text

WHY Most popular Concise Well documented and maintained Extensible via plugins Easy for designers

Slide 4

Slide 4 text

HOW Either self-host or include from CDN

Slide 5

Slide 5 text

CORE The core is the jQuery() function, a standard JavaScript function To save space, it’s aliased to $() Revert with jQuery.noConflict()

Slide 6

Slide 6 text

SELECTION Given this HTML element
Select it with jQuery("#menu") or simply $("#menu")

Slide 7

Slide 7 text

SELECTION

jQuery  Notes

  • Popular
  • Concise
  • Extensible

Slide 8

Slide 8 text

SELECTION

jQuery  Notes

  • Popular
  • Concise
  • Extensible
$("h1")

Slide 9

Slide 9 text

SELECTION

jQuery  Notes

  • Popular
  • Concise
  • Extensible
$(":header")

Slide 10

Slide 10 text

SELECTION

jQuery  Notes

  • Popular
  • Concise
  • Extensible
$("ul")

Slide 11

Slide 11 text

SELECTION

jQuery  Notes

  • Popular
  • Concise
  • Extensible
$(".benefits")

Slide 12

Slide 12 text

SELECTION

jQuery  Notes

  • Popular
  • Concise
  • Extensible
$("ul  li")

Slide 13

Slide 13 text

SELECTION

jQuery  Notes

  • Popular
  • Concise
  • Extensible
$("ul  li:eq(1)")

Slide 14

Slide 14 text

SELECTION

jQuery  Notes

  • Popular
  • Concise
  • Extensible
$("ul  li:not(:first)")

Slide 15

Slide 15 text

Accepts CSS 2 & 3 selectors, such as $("article  >  section") $(".menu  li:last-­‐child") $("a[href^='http://']") $("table  tr:nth-­‐child(2n+1)  td") Uses cross-browser Sizzle engine SELECTION

Slide 16

Slide 16 text

And custom extensions, such as :visible,  :hidden,  :focus,  :disabled :eq(),  :lt(),  :gt(),  :even,  :odd :empty,  :not(),  :has(),  :contains() :input,  :checkbox,  :radio,  :file :header,  :text,  :image CUSTOM

Slide 17

Slide 17 text

DIY Or make your own selectors $.expr[":"].parked =  function(obj){…}; and apply them $(".blues:parked").each(…);

Slide 18

Slide 18 text

OBJECTS Using jQuery with objects $(document) $(window) Define the current context $(this)

Slide 19

Slide 19 text

OBJECTS For example $("div").hover(function()  { $(this).addClass("on"); },  function()  {…});

Slide 20

Slide 20 text

CORE Code is generally run when DOM is ready window.onload  =  function(){…} $(document).ready(function(){…}); Can be called repeatedly, and shortened to $(function(){…});

Slide 21

Slide 21 text

CORE jQuery deals in ‘collections’ of one or more objects, so $("ul  li") returns a collection of all matching elements

Slide 22

Slide 22 text

CORE Some JavaScript properties work with collections $("ul  li").length As well as array indices to isolate individual DOM nodes $("ul  li")[0]

Slide 23

Slide 23 text

TIP When assigning jQuery collections to variables, prefix with $ var  $myList  =  $("#mylist"); Useful reminder as to a variable’s type.

Slide 24

Slide 24 text

CORE jQuery uses JavaScript syntax for conditional statements, loops, etc. if  (this  >  that)  { $("nav").hide(); }  else  {…}

Slide 25

Slide 25 text

METHODS Now for the cool stuff. Call jQuery methods to manipulate objects, data and collections $("ul  li").slideDown() $("ul  li").addClass()

Slide 26

Slide 26 text

METHODS Attribute Methods .val(),  .attr(),  .prop() .addClass(),  .removeClass() .hasClass(),  .toggleClass() and more…

Slide 27

Slide 27 text

METHODS

jQuery  Notes

  • Popular
  • Concise
  • Extensible

Slide 28

Slide 28 text

METHODS

jQuery  Notes

  • Popular
  • Concise
  • Extensible
$("ul").addClass("big")

Slide 29

Slide 29 text

METHODS

jQuery  Notes

  • Popular
  • Concise
  • Extensible
$("ul").addClass("big")

Slide 30

Slide 30 text

METHODS CSS / Dimension Methods .css(),  .height(),  .width() .innerHeight(),  outerHeight() .innerWidth(),  .outerWidth() .offset(),  .position() and more…

Slide 31

Slide 31 text

METHODS

jQuery  Notes

  • Popular
  • Concise
  • Extensible

Slide 32

Slide 32 text

METHODS

jQuery  Notes

  • Popular
  • Concise
  • Extensible
$("h1").css("color",  "lime")

Slide 33

Slide 33 text

METHODS

jQuery  Notes

  • Popular
  • Concise
  • … $("h1").css("color",  "lime")

Slide 34

Slide 34 text

METHODS Traversal Methods .each(),  .find(),  .filter() .children(),  .siblings(),  .end() .first(),  .last(),  .next(),  .prev() .parent(),  .andSelf(),  .closest() and more…

Slide 35

Slide 35 text

METHODS

jQuery  Notes

  • Popular
  • Concise
  • Extensible

Slide 36

Slide 36 text

METHODS

jQuery  Notes

  • Popular
  • Concise
  • Extensible
$(".benefits").prev()

Slide 37

Slide 37 text

METHODS

jQuery  Notes

  • Popular
  • Concise
  • Extensible
$(".benefits").prev()

Slide 38

Slide 38 text

METHODS

jQuery  Notes

  • Popular
  • Concise
  • Extensible
$("*").filter(":last-­‐child")

Slide 39

Slide 39 text

METHODS

jQuery  Notes

  • Popular
  • Concise
  • Extensible
$("*").filter(":last-­‐child")

Slide 40

Slide 40 text

METHODS Manipulation Methods .after(),  .before() .clone(),  .detach(),  .remove() .append(),  .prepend(),  .text()   .html(),  .wrap(),  .unwrap() and more…

Slide 41

Slide 41 text

METHODS

jQuery  Notes

  • Popular
  • Concise
  • Extensible

Slide 42

Slide 42 text

METHODS

jQuery  Notes

  • Popular
  • Concise
  • Extensible
$("ul").prepend("
  • First!
  • ")

    Slide 43

    Slide 43 text

    METHODS

    jQuery  Notes

    • First!
    • Popular
    • Concise
    • … $("ul").prepend("
    • First!
    • ")

    Slide 44

    Slide 44 text

    METHODS

    jQuery  Notes

    • Popular
    • Concise
    • Extensible
    $("ul").insertBefore("h1")

    Slide 45

    Slide 45 text

    METHODS
    • Popular
    • Concise
    • Extensible

    jQuery  Notes

    $("ul").insertBefore("h1")

    Slide 46

    Slide 46 text

    METHODS

    jQuery  Notes

    • Popular
    • Concise
    • Extensible
    $("li").unwrap()

    Slide 47

    Slide 47 text

    METHODS

    jQuery  Notes

  • Popular
  • Concise
  • Extensible
  • $("li").unwrap()

    Slide 48

    Slide 48 text

    METHODS Effects Methods .hide(),  .show() .animate(),  .delay(),  .stop() .fadeIn(),  .fadeOut(),  .fadeToggle() .slideUp(),  .slideDown() and more…

    Slide 49

    Slide 49 text

    METHODS Events Methods .click(),  .bind(),  .live() .focus(),  .blur(),  .hover() .mouseenter(),  .mouseleave()   .load(),  .resize(),  .scroll() and more…

    Slide 50

    Slide 50 text

    METHODS Ajax Methods .load(),  .ajax() .get(),  .post(),  .param() .getJSON(),  .getScript()   .serialize(),  .serializeArray() and more…

    Slide 51

    Slide 51 text

    METHODS if  (!document.ELEMENT_NODE)  {  document.ELEMENT_NODE  =  1;    document.ATTRIBUTE_NODE  =  2;  document.TEXT_NODE  =  3;   document.CDATA_SECTION_NODE  =  4;  document.ENTITY_REFERENCE_NODE  =  5;   document.ENTITY_NODE  =  6;  document.PROCESSING_INSTRUCTION_NODE  =  7;   document.COMMENT_NODE  =  8;  document.DOCUMENT_NODE  =  9;   document.DOCUMENT_TYPE_NODE  =  10;  document.DOCUMENT_FRAGMENT_NODE  =  11;   document.NOTATION_NODE  =  12;  }  document._importNode  =  function(node,   allChildren)  {  switch  (node.nodeType)  {  case  document.ELEMENT_NODE:  var   newNode  =  document.createElement(node.nodeName);  if  (node.attributes  &&   node.attributes.length  >  0)  for  (var  i  =  0;  il  =  node.attributes.length; i  <  il)  newNode.setAttribute(node.attributes[i].nodeName,   node.getAttribute(node.attributes[i++].nodeName)); if  (allChildren  &&  node.childNodes  &&  node.childNodes.length  >  0) for  (var  i  =  0;  il  =  node.childNodes.length;  i  <  il)   newNode.appendChild(document._importNode(node.childNodes[i++], allChildren));  return  newNode;  break;  case  document.TEXT_NODE: case  document.CDATA_SECTION_NODE:  case  document.COMMENT_NODE: return  document.createTextNode(node.nodeValue);  break;  }  }; www.alistapart.com/articles/crossbrowserscripting

    Slide 52

    Slide 52 text

    METHODS $("div").load("index.html");

    Slide 53

    Slide 53 text

    METHODS $("div").load("index.html  #main");

    Slide 54

    Slide 54 text

    METHODS As well as methods for… Array handling Forms manipulation File parsing Feature detection and more…

    Slide 55

    Slide 55 text

    CHAINING Most methods return the same jQuery collection, and can be chained in sequence $("div:hidden") .text("Error") .css("color","red") .fadeIn();

    Slide 56

    Slide 56 text

    CHAINING If a method returns a new collection, return to the previous collection using end() $("div").find("a") .addClass("mute") .end() .find("b").hide();

    Slide 57

    Slide 57 text

    CALLBACKS Some methods allow more code to be executed once they complete (a ‘callback’) $("div").animate( {top:  50}, function()  {…} );

    Slide 58

    Slide 58 text

    DEMO Given the following markup

    how might we show the user a success message above the text?

    Slide 59

    Slide 59 text

    DEMO One solution var  message  =  "Page  saved";       $("
    ",  {class:  "msg"})       .text(message)       .insertBefore("p")       .hide().fadeIn();

    Slide 60

    Slide 60 text

    DEMO Page saved Single-origin coffee viral aesthetic, jean shorts master cleanse tofu yr lo-fi chambray sartorial beard +1 retro photo booth. Pitchfork williamsburg beard vinyl wes anderson. Mlkshk brooklyn portland 3 wolf moon readymade iphone food truck. Austin lomo messenger bag, mcsweeney’s gentrify tattooed vegan fixie.

    Slide 61

    Slide 61 text

    $("
    ",  {class:  "msg"}) .text(message) .insertBefore("p").hide() .css("opacity",  0) .slideDown(function()  { $(this).css("opacity",  1) .hide().fadeIn();  }); BETTER?

    Slide 62

    Slide 62 text

    DEMO Page saved Single-origin coffee viral aesthetic, jean shorts master cleanse tofu yr lo-fi chambray sartorial beard +1 retro photo booth. Pitchfork williamsburg beard vinyl wes anderson. Mlkshk brooklyn portland 3 wolf moon readymade iphone food truck. Austin lomo messenger bag, mcsweeney’s gentrify tattooed vegan fixie.

    Slide 63

    Slide 63 text

    PLUGINS Plugins allow you to extend the jQuery model to include new methods. Galleries, lightboxes Form validation, input masks Layout control Drag & drop, sliders, calendars, etc.

    Slide 64

    Slide 64 text

    PLUGINS Creating your own plugin is easy $.fn.addIcon  =  function()  { return  this.prepend( $("",  {class:  "icon"}) ); }

    Slide 65

    Slide 65 text

    PLUGINS Creating your own plugin is easy $(":header").addIcon();

    Slide 66

    Slide 66 text

    PLUGINS Last week from Paravel and Chris Coyier, a plugin for fluid-width video embeds…

    Slide 67

    Slide 67 text

    WHERE jquery.com plugins.jquery.com jqapi.com code.google.com/apis/libraries/ fitvidsjs.com hipsteripsum.me

    Slide 68

    Slide 68 text

    Matthew Buchanan / @mrb matthewbuchanan.name www.cactuslab.com .end()