SlideShare una empresa de Scribd logo
1 de 68
0
TO HERO
 A jQuery Primer
   SEPTEMBER 2011
WHAT

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

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

  Either self-host or include from CDN

<head>
<script  src="http://code.jquery.com/
  jquery-­‐1.6.3.min.js"></script>
</head>
CORE

The core is the jQuery() function,
 a standard JavaScript function

 To save space, it’s aliased to $()

Revert with jQuery.noConflict()
SELECTION

  Given this HTML element
  <div  id="menu"></div>

Select it with jQuery("#menu")
    or simply $("#menu")
SELECTION

<h1>jQuery  Notes</h1>
<ul  class="benefits">
  <li>Popular</li>
  <li>Concise</li>
  <li>Extensible</li>
</ul>
SELECTION

<h1>jQuery  Notes</h1>
<ul  class="benefits">
  <li>Popular</li>
  <li>Concise</li>
  <li>Extensible</li>
</ul>

       $("h1")
SELECTION

<h1>jQuery  Notes</h1>
<ul  class="benefits">
  <li>Popular</li>
  <li>Concise</li>
  <li>Extensible</li>
</ul>

     $(":header")
SELECTION

<h1>jQuery  Notes</h1>
<ul  class="benefits">
  <li>Popular</li>
  <li>Concise</li>
  <li>Extensible</li>
</ul>

       $("ul")
SELECTION

<h1>jQuery  Notes</h1>
<ul  class="benefits">
  <li>Popular</li>
  <li>Concise</li>
  <li>Extensible</li>
</ul>

    $(".benefits")
SELECTION

<h1>jQuery  Notes</h1>
<ul  class="benefits">
  <li>Popular</li>
  <li>Concise</li>
  <li>Extensible</li>
</ul>

      $("ul  li")
SELECTION

<h1>jQuery  Notes</h1>
<ul  class="benefits">
  <li>Popular</li>
  <li>Concise</li>
  <li>Extensible</li>
</ul>

   $("ul  li:eq(1)")
SELECTION

<h1>jQuery  Notes</h1>
<ul  class="benefits">
  <li>Popular</li>
  <li>Concise</li>
  <li>Extensible</li>
</ul>

$("ul  li:not(:first)")
SELECTION

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
CUSTOM

     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
DIY

 Or make your own selectors
   $.expr[":"].parked
   =  function(obj){…};

      and apply them
$(".blues:parked").each(…);
OBJECTS

Using jQuery with objects
      $(document)
       $(window)


Define the current context
        $(this)
OBJECTS

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

Code is generally run when DOM is ready
    window.onload  =  function(){…}
  $(document).ready(function(){…});


Can be called repeatedly, and shortened to
           $(function(){…});
CORE

jQuery deals in ‘collections’ of
   one or more objects, so

         $("ul  li")

  returns a collection of all
     matching elements
CORE

  Some JavaScript properties
    work with collections
      $("ul  li").length

As well as array indices to isolate
    individual DOM nodes
         $("ul  li")[0]
TIP

When assigning jQuery collections
   to variables, prefix with $

var  $myList  =  $("#mylist");

  Useful reminder as to a variable’s type.
CORE

jQuery uses JavaScript syntax for
conditional statements, loops, etc.

      if  (this  >  that)  {
         $("nav").hide();
      }  else  {…}
METHODS

      Now for the cool stuff.

Call jQuery methods to manipulate
   objects, data and collections

    $("ul  li").slideDown()
    $("ul  li").addClass()
METHODS

     Attribute Methods

 .val(),  .attr(),  .prop()
.addClass(),  .removeClass()
.hasClass(),  .toggleClass()

          and more…
METHODS

<h1>jQuery  Notes</h1>
<ul  class="benefits">
  <li>Popular</li>
  <li>Concise</li>
  <li>Extensible</li>
</ul>
METHODS

<h1>jQuery  Notes</h1>
<ul  class="benefits">
  <li>Popular</li>
  <li>Concise</li>
  <li>Extensible</li>
</ul>

$("ul").addClass("big")
METHODS

<h1>jQuery  Notes</h1>
<ul  class="benefits  big">
  <li>Popular</li>
  <li>Concise</li>
  <li>Extensible</li>
</ul>

$("ul").addClass("big")
METHODS

   CSS / Dimension Methods

 .css(),  .height(),  .width()
.innerHeight(),  outerHeight()
.innerWidth(),  .outerWidth()
   .offset(),  .position()

           and more…
METHODS

<h1>jQuery  Notes</h1>
<ul  class="benefits">
  <li>Popular</li>
  <li>Concise</li>
  <li>Extensible</li>
</ul>
METHODS

   <h1>jQuery  Notes</h1>
   <ul  class="benefits">
     <li>Popular</li>
     <li>Concise</li>
     <li>Extensible</li>
   </ul>

$("h1").css("color",  "lime")
METHODS

   <h1  style="color:lime">
     jQuery  Notes</h1>
   <ul  class="benefits">
     <li>Popular</li>
     <li>Concise</li>
     …

$("h1").css("color",  "lime")
METHODS

          Traversal Methods

    .each(),  .find(),  .filter()
 .children(),  .siblings(),  .end()
.first(),  .last(),  .next(),  .prev()
 .parent(),  .andSelf(),  .closest()

               and more…
METHODS

<h1>jQuery  Notes</h1>
<ul  class="benefits">
  <li>Popular</li>
  <li>Concise</li>
  <li>Extensible</li>
</ul>
METHODS

<h1>jQuery  Notes</h1>
<ul  class="benefits">
  <li>Popular</li>
  <li>Concise</li>
  <li>Extensible</li>
</ul>

$(".benefits").prev()
METHODS

<h1>jQuery  Notes</h1>
<ul  class="benefits">
  <li>Popular</li>
  <li>Concise</li>
  <li>Extensible</li>
</ul>

$(".benefits").prev()
METHODS

   <h1>jQuery  Notes</h1>
   <ul  class="benefits">
     <li>Popular</li>
     <li>Concise</li>
     <li>Extensible</li>
   </ul>

$("*").filter(":last-­‐child")
METHODS

   <h1>jQuery  Notes</h1>
   <ul  class="benefits">
     <li>Popular</li>
     <li>Concise</li>
     <li>Extensible</li>
   </ul>

$("*").filter(":last-­‐child")
METHODS

     Manipulation Methods

      .after(),  .before()
.clone(),  .detach(),  .remove()
.append(),  .prepend(),  .text()  
 .html(),  .wrap(),  .unwrap()

            and more…
METHODS

<h1>jQuery  Notes</h1>
<ul  class="benefits">
  <li>Popular</li>
  <li>Concise</li>
  <li>Extensible</li>
</ul>
METHODS

     <h1>jQuery  Notes</h1>
     <ul  class="benefits">
       <li>Popular</li>
       <li>Concise</li>
       <li>Extensible</li>
     </ul>

$("ul").prepend("<li>First!</li>")
METHODS

     <h1>jQuery  Notes</h1>
     <ul  class="benefits">
       <li>First!</li>
       <li>Popular</li>
       <li>Concise</li>
     …

$("ul").prepend("<li>First!</li>")
METHODS

 <h1>jQuery  Notes</h1>
 <ul  class="benefits">
   <li>Popular</li>
   <li>Concise</li>
   <li>Extensible</li>
 </ul>

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

 <ul  class="benefits">
   <li>Popular</li>
   <li>Concise</li>
   <li>Extensible</li>
 </ul>
 <h1>jQuery  Notes</h1>

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

<h1>jQuery  Notes</h1>
<ul  class="benefits">
  <li>Popular</li>
  <li>Concise</li>
  <li>Extensible</li>
</ul>

   $("li").unwrap()
METHODS

<h1>jQuery  Notes</h1>
<li>Popular</li>
<li>Concise</li>
<li>Extensible</li>



   $("li").unwrap()
METHODS

           Effects Methods

          .hide(),  .show()
   .animate(),  .delay(),  .stop()
.fadeIn(),  .fadeOut(),  .fadeToggle()
      .slideUp(),  .slideDown()

               and more…
METHODS

        Events Methods

 .click(),  .bind(),  .live()
 .focus(),  .blur(),  .hover()
.mouseenter(),  .mouseleave()  
.load(),  .resize(),  .scroll()

           and more…
METHODS

          Ajax Methods

       .load(),  .ajax()
   .get(),  .post(),  .param()
   .getJSON(),  .getScript()  
.serialize(),  .serializeArray()

            and more…
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
METHODS



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



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

As well as methods for…

   Array handling
 Forms manipulation
    File parsing
  Feature detection
       and more…
CHAINING

  Most methods return the same jQuery
collection, and can be chained in sequence

        $("div:hidden")
          .text("Error")
          .css("color","red")
          .fadeIn();
CHAINING

If a method returns a new collection, return
    to the previous collection using end()

          $("div").find("a")
             .addClass("mute")
             .end()
             .find("b").hide();
CALLBACKS

  Some methods allow more code to be
executed once they complete (a ‘callback’)

          $("div").animate(
               {top:  50},
               function()  {…}
          );
DEMO

 Given the following markup

          <p>…</p>


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

                     One solution

             var  message  =  "Page  saved";
             $("<div>",  {class:  "msg"})
                .text(message)
                .insertBefore("p")
                .hide().fadeIn();
DEMO
Single-origin coffee viral aesthetic, jean shorts master
  Page saved
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.
BETTER?

$("<div>",  {class:  "msg"})
  .text(message)
  .insertBefore("p").hide()
  .css("opacity",  0)
  .slideDown(function()  {
    $(this).css("opacity",  1)
    .hide().fadeIn();  });
DEMO
Single-origin coffee viral aesthetic, jean shorts master
  Page saved
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.
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.
PLUGINS

    Creating your own plugin is easy

$.fn.addIcon  =  function()  {
    return  this.prepend(
         $("<span>",  {class:  "icon"})
    );
}
PLUGINS

Creating your own plugin is easy

   $(":header").addIcon();
PLUGINS

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

          jquery.com
      plugins.jquery.com
          jqapi.com
code.google.com/apis/libraries/
         fitvidsjs.com
       hipsteripsum.me
.end()
Matthew Buchanan / @mrb
    matthewbuchanan.name
      www.cactuslab.com

Más contenido relacionado

La actualidad más candente

GDI Seattle - Intro to JavaScript Class 4
GDI Seattle - Intro to JavaScript Class 4GDI Seattle - Intro to JavaScript Class 4
GDI Seattle - Intro to JavaScript Class 4Heather Rock
 
DOM Scripting Toolkit - jQuery
DOM Scripting Toolkit - jQueryDOM Scripting Toolkit - jQuery
DOM Scripting Toolkit - jQueryRemy Sharp
 
Crazy things done on PHP
Crazy things done on PHPCrazy things done on PHP
Crazy things done on PHPTaras Kalapun
 
Hacking Your Way To Better Security - Dutch PHP Conference 2016
Hacking Your Way To Better Security - Dutch PHP Conference 2016Hacking Your Way To Better Security - Dutch PHP Conference 2016
Hacking Your Way To Better Security - Dutch PHP Conference 2016Colin O'Dell
 
Bag Of Tricks From Iusethis
Bag Of Tricks From IusethisBag Of Tricks From Iusethis
Bag Of Tricks From IusethisMarcus Ramberg
 
jQuery: Nuts, Bolts and Bling
jQuery: Nuts, Bolts and BlingjQuery: Nuts, Bolts and Bling
jQuery: Nuts, Bolts and BlingDoug Neiner
 
DOSUG Intro to JQuery JavaScript Framework
DOSUG Intro to JQuery JavaScript FrameworkDOSUG Intro to JQuery JavaScript Framework
DOSUG Intro to JQuery JavaScript FrameworkMatthew McCullough
 
Using jQuery to Extend CSS
Using jQuery to Extend CSSUsing jQuery to Extend CSS
Using jQuery to Extend CSSChris Coyier
 
Modern JavaScript Engine Performance
Modern JavaScript Engine PerformanceModern JavaScript Engine Performance
Modern JavaScript Engine PerformanceCatalin Dumitru
 
Symfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technologySymfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technologyDaniel Knell
 
Styles tagsets
Styles tagsetsStyles tagsets
Styles tagsetseagebhart
 
Rage Against the Framework
Rage Against the FrameworkRage Against the Framework
Rage Against the FrameworkDavid Ortinau
 
Using Templates to Achieve Awesomer Architecture
Using Templates to Achieve Awesomer ArchitectureUsing Templates to Achieve Awesomer Architecture
Using Templates to Achieve Awesomer ArchitectureGarann Means
 
Your code sucks, let's fix it - DPC UnCon
Your code sucks, let's fix it - DPC UnConYour code sucks, let's fix it - DPC UnCon
Your code sucks, let's fix it - DPC UnConRafael Dohms
 
Modern Application Foundations: Underscore and Twitter Bootstrap
Modern Application Foundations: Underscore and Twitter BootstrapModern Application Foundations: Underscore and Twitter Bootstrap
Modern Application Foundations: Underscore and Twitter BootstrapHoward Lewis Ship
 
GQL cheat sheet latest
GQL cheat sheet latestGQL cheat sheet latest
GQL cheat sheet latestsones GmbH
 

La actualidad más candente (20)

GDI Seattle - Intro to JavaScript Class 4
GDI Seattle - Intro to JavaScript Class 4GDI Seattle - Intro to JavaScript Class 4
GDI Seattle - Intro to JavaScript Class 4
 
DOM Scripting Toolkit - jQuery
DOM Scripting Toolkit - jQueryDOM Scripting Toolkit - jQuery
DOM Scripting Toolkit - jQuery
 
Crazy things done on PHP
Crazy things done on PHPCrazy things done on PHP
Crazy things done on PHP
 
Hacking Your Way To Better Security - Dutch PHP Conference 2016
Hacking Your Way To Better Security - Dutch PHP Conference 2016Hacking Your Way To Better Security - Dutch PHP Conference 2016
Hacking Your Way To Better Security - Dutch PHP Conference 2016
 
Bag Of Tricks From Iusethis
Bag Of Tricks From IusethisBag Of Tricks From Iusethis
Bag Of Tricks From Iusethis
 
Backbone - TDC 2011 Floripa
Backbone - TDC 2011 FloripaBackbone - TDC 2011 Floripa
Backbone - TDC 2011 Floripa
 
jQuery: Nuts, Bolts and Bling
jQuery: Nuts, Bolts and BlingjQuery: Nuts, Bolts and Bling
jQuery: Nuts, Bolts and Bling
 
DOSUG Intro to JQuery JavaScript Framework
DOSUG Intro to JQuery JavaScript FrameworkDOSUG Intro to JQuery JavaScript Framework
DOSUG Intro to JQuery JavaScript Framework
 
jquery
jqueryjquery
jquery
 
PHP API
PHP APIPHP API
PHP API
 
Using jQuery to Extend CSS
Using jQuery to Extend CSSUsing jQuery to Extend CSS
Using jQuery to Extend CSS
 
Modern JavaScript Engine Performance
Modern JavaScript Engine PerformanceModern JavaScript Engine Performance
Modern JavaScript Engine Performance
 
Symfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technologySymfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technology
 
Styles tagsets
Styles tagsetsStyles tagsets
Styles tagsets
 
BVJS
BVJSBVJS
BVJS
 
Rage Against the Framework
Rage Against the FrameworkRage Against the Framework
Rage Against the Framework
 
Using Templates to Achieve Awesomer Architecture
Using Templates to Achieve Awesomer ArchitectureUsing Templates to Achieve Awesomer Architecture
Using Templates to Achieve Awesomer Architecture
 
Your code sucks, let's fix it - DPC UnCon
Your code sucks, let's fix it - DPC UnConYour code sucks, let's fix it - DPC UnCon
Your code sucks, let's fix it - DPC UnCon
 
Modern Application Foundations: Underscore and Twitter Bootstrap
Modern Application Foundations: Underscore and Twitter BootstrapModern Application Foundations: Underscore and Twitter Bootstrap
Modern Application Foundations: Underscore and Twitter Bootstrap
 
GQL cheat sheet latest
GQL cheat sheet latestGQL cheat sheet latest
GQL cheat sheet latest
 

Similar a Zero to Hero, a jQuery Primer

jQuery: Tips, tricks and hints for better development and Performance
jQuery: Tips, tricks and hints for better development and PerformancejQuery: Tips, tricks and hints for better development and Performance
jQuery: Tips, tricks and hints for better development and PerformanceJonas De Smet
 
Jquery tutorial
Jquery tutorialJquery tutorial
Jquery tutorialBui Kiet
 
Jquery Complete Presentation along with Javascript Basics
Jquery Complete Presentation along with Javascript BasicsJquery Complete Presentation along with Javascript Basics
Jquery Complete Presentation along with Javascript BasicsEPAM Systems
 
Introduction to jQuery
Introduction to jQueryIntroduction to jQuery
Introduction to jQuerySeble Nigussie
 
Introduzione JQuery
Introduzione JQueryIntroduzione JQuery
Introduzione JQueryorestJump
 
Jquery presentation
Jquery presentationJquery presentation
Jquery presentationguest5d87aa6
 
presentation_jquery_ppt.pptx
presentation_jquery_ppt.pptxpresentation_jquery_ppt.pptx
presentation_jquery_ppt.pptxazz71
 
Advanced JQuery Mobile tutorial with Phonegap
Advanced JQuery Mobile tutorial with Phonegap Advanced JQuery Mobile tutorial with Phonegap
Advanced JQuery Mobile tutorial with Phonegap Rakesh Jha
 
HTML5 and the dawn of rich mobile web applications pt 2
HTML5 and the dawn of rich mobile web applications pt 2HTML5 and the dawn of rich mobile web applications pt 2
HTML5 and the dawn of rich mobile web applications pt 2James Pearce
 
J query b_dotnet_ug_meet_12_may_2012
J query b_dotnet_ug_meet_12_may_2012J query b_dotnet_ug_meet_12_may_2012
J query b_dotnet_ug_meet_12_may_2012ghnash
 
Advanced jQuery
Advanced jQueryAdvanced jQuery
Advanced jQuerysergioafp
 
Unobtrusive javascript with jQuery
Unobtrusive javascript with jQueryUnobtrusive javascript with jQuery
Unobtrusive javascript with jQueryAngel Ruiz
 
jQuery Rescue Adventure
jQuery Rescue AdventurejQuery Rescue Adventure
jQuery Rescue AdventureAllegient
 

Similar a Zero to Hero, a jQuery Primer (20)

J query training
J query trainingJ query training
J query training
 
jQuery
jQueryjQuery
jQuery
 
jQuery: Tips, tricks and hints for better development and Performance
jQuery: Tips, tricks and hints for better development and PerformancejQuery: Tips, tricks and hints for better development and Performance
jQuery: Tips, tricks and hints for better development and Performance
 
jQuery quick tuts
jQuery quick tutsjQuery quick tuts
jQuery quick tuts
 
Jquery tutorial
Jquery tutorialJquery tutorial
Jquery tutorial
 
Jquery Complete Presentation along with Javascript Basics
Jquery Complete Presentation along with Javascript BasicsJquery Complete Presentation along with Javascript Basics
Jquery Complete Presentation along with Javascript Basics
 
Introduction to jQuery
Introduction to jQueryIntroduction to jQuery
Introduction to jQuery
 
Introduzione JQuery
Introduzione JQueryIntroduzione JQuery
Introduzione JQuery
 
Jquery presentation
Jquery presentationJquery presentation
Jquery presentation
 
presentation_jquery_ppt.pptx
presentation_jquery_ppt.pptxpresentation_jquery_ppt.pptx
presentation_jquery_ppt.pptx
 
jQuery
jQueryjQuery
jQuery
 
Advanced JQuery Mobile tutorial with Phonegap
Advanced JQuery Mobile tutorial with Phonegap Advanced JQuery Mobile tutorial with Phonegap
Advanced JQuery Mobile tutorial with Phonegap
 
jQuery Basic API
jQuery Basic APIjQuery Basic API
jQuery Basic API
 
HTML5 and the dawn of rich mobile web applications pt 2
HTML5 and the dawn of rich mobile web applications pt 2HTML5 and the dawn of rich mobile web applications pt 2
HTML5 and the dawn of rich mobile web applications pt 2
 
J query b_dotnet_ug_meet_12_may_2012
J query b_dotnet_ug_meet_12_may_2012J query b_dotnet_ug_meet_12_may_2012
J query b_dotnet_ug_meet_12_may_2012
 
Jquery 5
Jquery 5Jquery 5
Jquery 5
 
jQuery Essentials
jQuery EssentialsjQuery Essentials
jQuery Essentials
 
Advanced jQuery
Advanced jQueryAdvanced jQuery
Advanced jQuery
 
Unobtrusive javascript with jQuery
Unobtrusive javascript with jQueryUnobtrusive javascript with jQuery
Unobtrusive javascript with jQuery
 
jQuery Rescue Adventure
jQuery Rescue AdventurejQuery Rescue Adventure
jQuery Rescue Adventure
 

Último

Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfhans926745
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 

Último (20)

Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 

Zero to Hero, a jQuery Primer

  • 1. 0 TO HERO A jQuery Primer SEPTEMBER 2011
  • 2. WHAT JavaScript framework Cross-browser compatible Lightweight (31KB) APIs for DOM, events, animation, Ajax
  • 3. WHY Most popular Concise Well documented and maintained Extensible via plugins Easy for designers
  • 4. HOW Either self-host or include from CDN <head> <script  src="http://code.jquery.com/ jquery-­‐1.6.3.min.js"></script> </head>
  • 5. CORE The core is the jQuery() function, a standard JavaScript function To save space, it’s aliased to $() Revert with jQuery.noConflict()
  • 6. SELECTION Given this HTML element <div  id="menu"></div> Select it with jQuery("#menu") or simply $("#menu")
  • 7. SELECTION <h1>jQuery  Notes</h1> <ul  class="benefits"> <li>Popular</li> <li>Concise</li> <li>Extensible</li> </ul>
  • 8. SELECTION <h1>jQuery  Notes</h1> <ul  class="benefits"> <li>Popular</li> <li>Concise</li> <li>Extensible</li> </ul> $("h1")
  • 9. SELECTION <h1>jQuery  Notes</h1> <ul  class="benefits"> <li>Popular</li> <li>Concise</li> <li>Extensible</li> </ul> $(":header")
  • 10. SELECTION <h1>jQuery  Notes</h1> <ul  class="benefits"> <li>Popular</li> <li>Concise</li> <li>Extensible</li> </ul> $("ul")
  • 11. SELECTION <h1>jQuery  Notes</h1> <ul  class="benefits"> <li>Popular</li> <li>Concise</li> <li>Extensible</li> </ul> $(".benefits")
  • 12. SELECTION <h1>jQuery  Notes</h1> <ul  class="benefits"> <li>Popular</li> <li>Concise</li> <li>Extensible</li> </ul> $("ul  li")
  • 13. SELECTION <h1>jQuery  Notes</h1> <ul  class="benefits"> <li>Popular</li> <li>Concise</li> <li>Extensible</li> </ul> $("ul  li:eq(1)")
  • 14. SELECTION <h1>jQuery  Notes</h1> <ul  class="benefits"> <li>Popular</li> <li>Concise</li> <li>Extensible</li> </ul> $("ul  li:not(:first)")
  • 15. SELECTION 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
  • 16. CUSTOM 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
  • 17. DIY Or make your own selectors $.expr[":"].parked =  function(obj){…}; and apply them $(".blues:parked").each(…);
  • 18. OBJECTS Using jQuery with objects $(document) $(window) Define the current context $(this)
  • 19. OBJECTS For example $("div").hover(function()  { $(this).addClass("on"); },  function()  {…});
  • 20. CORE Code is generally run when DOM is ready window.onload  =  function(){…} $(document).ready(function(){…}); Can be called repeatedly, and shortened to $(function(){…});
  • 21. CORE jQuery deals in ‘collections’ of one or more objects, so $("ul  li") returns a collection of all matching elements
  • 22. CORE Some JavaScript properties work with collections $("ul  li").length As well as array indices to isolate individual DOM nodes $("ul  li")[0]
  • 23. TIP When assigning jQuery collections to variables, prefix with $ var  $myList  =  $("#mylist"); Useful reminder as to a variable’s type.
  • 24. CORE jQuery uses JavaScript syntax for conditional statements, loops, etc. if  (this  >  that)  { $("nav").hide(); }  else  {…}
  • 25. METHODS Now for the cool stuff. Call jQuery methods to manipulate objects, data and collections $("ul  li").slideDown() $("ul  li").addClass()
  • 26. METHODS Attribute Methods .val(),  .attr(),  .prop() .addClass(),  .removeClass() .hasClass(),  .toggleClass() and more…
  • 27. METHODS <h1>jQuery  Notes</h1> <ul  class="benefits"> <li>Popular</li> <li>Concise</li> <li>Extensible</li> </ul>
  • 28. METHODS <h1>jQuery  Notes</h1> <ul  class="benefits"> <li>Popular</li> <li>Concise</li> <li>Extensible</li> </ul> $("ul").addClass("big")
  • 29. METHODS <h1>jQuery  Notes</h1> <ul  class="benefits  big"> <li>Popular</li> <li>Concise</li> <li>Extensible</li> </ul> $("ul").addClass("big")
  • 30. METHODS CSS / Dimension Methods .css(),  .height(),  .width() .innerHeight(),  outerHeight() .innerWidth(),  .outerWidth() .offset(),  .position() and more…
  • 31. METHODS <h1>jQuery  Notes</h1> <ul  class="benefits"> <li>Popular</li> <li>Concise</li> <li>Extensible</li> </ul>
  • 32. METHODS <h1>jQuery  Notes</h1> <ul  class="benefits"> <li>Popular</li> <li>Concise</li> <li>Extensible</li> </ul> $("h1").css("color",  "lime")
  • 33. METHODS <h1  style="color:lime"> jQuery  Notes</h1> <ul  class="benefits"> <li>Popular</li> <li>Concise</li> … $("h1").css("color",  "lime")
  • 34. METHODS Traversal Methods .each(),  .find(),  .filter() .children(),  .siblings(),  .end() .first(),  .last(),  .next(),  .prev() .parent(),  .andSelf(),  .closest() and more…
  • 35. METHODS <h1>jQuery  Notes</h1> <ul  class="benefits"> <li>Popular</li> <li>Concise</li> <li>Extensible</li> </ul>
  • 36. METHODS <h1>jQuery  Notes</h1> <ul  class="benefits"> <li>Popular</li> <li>Concise</li> <li>Extensible</li> </ul> $(".benefits").prev()
  • 37. METHODS <h1>jQuery  Notes</h1> <ul  class="benefits"> <li>Popular</li> <li>Concise</li> <li>Extensible</li> </ul> $(".benefits").prev()
  • 38. METHODS <h1>jQuery  Notes</h1> <ul  class="benefits"> <li>Popular</li> <li>Concise</li> <li>Extensible</li> </ul> $("*").filter(":last-­‐child")
  • 39. METHODS <h1>jQuery  Notes</h1> <ul  class="benefits"> <li>Popular</li> <li>Concise</li> <li>Extensible</li> </ul> $("*").filter(":last-­‐child")
  • 40. METHODS Manipulation Methods .after(),  .before() .clone(),  .detach(),  .remove() .append(),  .prepend(),  .text()   .html(),  .wrap(),  .unwrap() and more…
  • 41. METHODS <h1>jQuery  Notes</h1> <ul  class="benefits"> <li>Popular</li> <li>Concise</li> <li>Extensible</li> </ul>
  • 42. METHODS <h1>jQuery  Notes</h1> <ul  class="benefits"> <li>Popular</li> <li>Concise</li> <li>Extensible</li> </ul> $("ul").prepend("<li>First!</li>")
  • 43. METHODS <h1>jQuery  Notes</h1> <ul  class="benefits"> <li>First!</li> <li>Popular</li> <li>Concise</li> … $("ul").prepend("<li>First!</li>")
  • 44. METHODS <h1>jQuery  Notes</h1> <ul  class="benefits"> <li>Popular</li> <li>Concise</li> <li>Extensible</li> </ul> $("ul").insertBefore("h1")
  • 45. METHODS <ul  class="benefits"> <li>Popular</li> <li>Concise</li> <li>Extensible</li> </ul> <h1>jQuery  Notes</h1> $("ul").insertBefore("h1")
  • 46. METHODS <h1>jQuery  Notes</h1> <ul  class="benefits"> <li>Popular</li> <li>Concise</li> <li>Extensible</li> </ul> $("li").unwrap()
  • 48. METHODS Effects Methods .hide(),  .show() .animate(),  .delay(),  .stop() .fadeIn(),  .fadeOut(),  .fadeToggle() .slideUp(),  .slideDown() and more…
  • 49. METHODS Events Methods .click(),  .bind(),  .live() .focus(),  .blur(),  .hover() .mouseenter(),  .mouseleave()   .load(),  .resize(),  .scroll() and more…
  • 50. METHODS Ajax Methods .load(),  .ajax() .get(),  .post(),  .param() .getJSON(),  .getScript()   .serialize(),  .serializeArray() and more…
  • 51. 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
  • 54. METHODS As well as methods for… Array handling Forms manipulation File parsing Feature detection and more…
  • 55. CHAINING Most methods return the same jQuery collection, and can be chained in sequence $("div:hidden") .text("Error") .css("color","red") .fadeIn();
  • 56. CHAINING If a method returns a new collection, return to the previous collection using end() $("div").find("a") .addClass("mute") .end() .find("b").hide();
  • 57. CALLBACKS Some methods allow more code to be executed once they complete (a ‘callback’) $("div").animate( {top:  50}, function()  {…} );
  • 58. DEMO Given the following markup <p>…</p> how might we show the user a success message above the text?
  • 59. DEMO One solution var  message  =  "Page  saved";       $("<div>",  {class:  "msg"})       .text(message)       .insertBefore("p")       .hide().fadeIn();
  • 60. DEMO Single-origin coffee viral aesthetic, jean shorts master Page saved 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.
  • 61. BETTER? $("<div>",  {class:  "msg"}) .text(message) .insertBefore("p").hide() .css("opacity",  0) .slideDown(function()  { $(this).css("opacity",  1) .hide().fadeIn();  });
  • 62. DEMO Single-origin coffee viral aesthetic, jean shorts master Page saved 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.
  • 63. 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.
  • 64. PLUGINS Creating your own plugin is easy $.fn.addIcon  =  function()  { return  this.prepend( $("<span>",  {class:  "icon"}) ); }
  • 65. PLUGINS Creating your own plugin is easy $(":header").addIcon();
  • 66. PLUGINS Last week from Paravel and Chris Coyier, a plugin for fluid-width video embeds…
  • 67. WHERE jquery.com plugins.jquery.com jqapi.com code.google.com/apis/libraries/ fitvidsjs.com hipsteripsum.me
  • 68. .end() Matthew Buchanan / @mrb matthewbuchanan.name www.cactuslab.com