SlideShare a Scribd company logo
jQuery Fundamentals
    rebecca@rebeccamurphey.com
         @rmurphey on Twitter
http://delicious.com/rdmey/jquery-class
Housekeeping
• ftp: [yourname].jqueryclasses.com

• username: yourname@jqueryclasses.com

• password: jquery!class
In the beginning,
there was JavaScript
             http://www.flickr.com/photos/freeparking/476376873/
Enter jQuery




               http://www.flickr.com/photos/timsnell/513274898/
• Reads   like what you mean

• Simplifies   cross-browser issues

• Huge    community & plugin ecosystem
JavaScript 101
Because there’s lots of stuff you still need to know.
Operators and logic
Objects


• Booleans, strings, numbers,
 arrays, and functions are all
 objects

• Objects are a good way of
 organizing your code



                                 http://www.flickr.com/photos/oxido1180/2685774194/
true



a boolean
Booleans (true/false)
• Lots    of things are “truthy”

  • true

  • '0'   (or any non-empty string)

  •1     (or any non-zero number)

  • any    array, even empty ones

  • any    object, even empty ones
Booleans (true/false)
•   A few things are “falsy”

    • false

    •0

    • undefined

    • null

    • ''   (an empty, zero-length string)

    • NaN      (“not a number,” the result of illegal math operations)
‘hello’



a string
42



a number
['hello', 'world']



an array
function() { }



a function
Functions
• Functions   can take arguments, but they don't have to

• Functionscan return anything, including other functions -- but
 they don't have to return anything

• Functions   can be assigned to variables

• Functions
         can be passed to other functions by name, or as
 anonymous functions

• Functions   are normally called by putting () after their name
Variable scope




                 http://www.flickr.com/photos/8/12978541/
Closures




           http://www.flickr.com/photos/gadl/272562772/
jQuery
In a nutshell


• Get   some elements

• Do    something to them




                            http://www.flickr.com/photos/exfordy/1184487050/
$()
Selectors
http://docs.jquery.com/Selectors
Chaining
           http://www.flickr.com/photos/itsgreg/315015695/
Events
        http://docs.jquery.com/Events
http://docs.jquery.com/Events/jQuery.Event
$('a.foo').click(function(e) {

 e.preventDefault();

 var $this = $(this);

 $this.css({ color : 'red' });

 console.log('You clicked a link that points to ' +

 
 $this.attr('href'));
});




when an a.foo link is clicked, prevent the default action,
color the link red, and tell the user where the link pointed to
CSS
Manipulating
          http://www.flickr.com/photos/nnova/3373350948/
Traversing
        http://www.flickr.com/photos/jenny-pics/3258777653/
Effects
          http://www.flickr.com/photos/foxypar4/2153422313/
XHR
      http://www.flickr.com/photos/timsnell/513274898/
XHR basics
• type: GET     or POST

• url: a   fully qualified or relative URL

• data: object   or query string

• dataType: text, html, xml, json, jsonp, script

• callback: function   to run when data is received
POST or GET?
• POST  when you want to
 change something on the
 server

• GET when you just want to
 get something from the
 server




                              http://www.flickr.com/photos/14511253@N04/3298001461/
POST or GET?
• POST  when you want to
 change something on the
 server

• GET when you just want to
 get something from the
 server




                              http://www.flickr.com/photos/14511253@N04/3298001461/
More options with $.ajax()
• async

• error   callback

• timeout

• cache

• header        modification

• more    ...

                              http://www.flickr.com/photos/jeet_sen/1691759831/
XHR events
• ajaxStart

• ajaxStop

• ajaxComplete

• ajaxError

• ajaxSend

• ajaxSuccess
$('#loading')

 .ajaxStart(function() {

 
 $(this).show();

 })

 .ajaxStop(function() {

 
 $(this).hide();

 });
Utility Methods
          http://www.flickr.com/photos/geoftheref/2486112196/
Plugin authoring
Plugins
• Extend   core jQuery

 • $.myPlugin()

• Extend   jQuery object methods

 • $('#foo')
   .myPlugin()
Extending object methods
• Chainability

• Options   & defaults

• Callbacks
jQuery.fn.myPlugin = function(options) {

 jQuery.fn.myPlugin.defaults = {

 
 // ...

 };


    var settings = $.extend(

    
 jQuery.fn.myPlugin.defaults,

    
 options

    );


    return this.each(function() {

    
 // ...

    });

};




basic concept of a jQuery object method
jQuery.fn.myPlugin = function(options) {

 jQuery.fn.myPlugin.defaults = {

 
 // ...

 };


    var settings = $.extend(

    
 jQuery.fn.myPlugin.defaults,

    
 options

    );


    return this.each(function() {

    
 // ...

    });

};




naming the plugin
jQuery.fn.myPlugin = function(options) {

 jQuery.fn.myPlugin.defaults = {

 
 // ...

 };


    var settings = $.extend(

    
 jQuery.fn.myPlugin.defaults,

    
 options

    );


    return this.each(function() {

    
 // ...

    });

};




setting the defaults for the plugin
jQuery.fn.myPlugin = function(options) {

 jQuery.fn.myPlugin.defaults = {

 
 // ...

 };


    var settings = $.extend(

    
 jQuery.fn.myPlugin.defaults,

    
 options

    );


    return this.each(function() {

    
 // ...

    });

};




overriding the defaults with user-defined options
jQuery.fn.myPlugin = function(options) {

 jQuery.fn.myPlugin.defaults = {

 
 // ...

 };


    var settings = $.extend(

    
 jQuery.fn.myPlugin.defaults,

    
 options

    );


    return this.each(function() {

    
 // ...

    });

};




enabling chaining
jQuery.fn.hrefToContents = function(options) {


    return this.each(function() {

    
 $(this).text($(this).attr('href'));

    });

};




a plugin that requires iteration
jQuery.fn.hoverClass = function(c) {

 return this.hover(

 
 function() {

 
 
 $(this).addClass(c);

 
 },

 
 function() {

 
 
 $(this).removeClass(c);

 
 }

 );
};




a plugin that doesn't require iteration
jQuery.fn.myPlugin = function(options) {

 jQuery.fn.myPlugin.defaults = {

 
 callback : function() { alert('done'); }

 };


    var settings = $.extend(

    
 jQuery.fn.myPlugin.defaults,

    
 options

    );


    this.each(function() {

    
 // ...

    });


    jQuery.fn.myPlugin.defaults.callback();


    return this;
};



adding a callback

More Related Content

KEY
jQuery Namespace Pattern
PDF
Beyond the DOM: Sane Structure for JS Apps
PDF
Intro programacion funcional
PDF
Functionality Focused Code Organization
PDF
Building Large jQuery Applications
PDF
Mulberry: A Mobile App Development Toolkit
PDF
A New Baseline for Front-End Devs
jQuery Namespace Pattern
Beyond the DOM: Sane Structure for JS Apps
Intro programacion funcional
Functionality Focused Code Organization
Building Large jQuery Applications
Mulberry: A Mobile App Development Toolkit
A New Baseline for Front-End Devs

What's hot (20)

PDF
Delivering a Responsive UI
PDF
Dojo Confessions
PDF
How Kris Writes Symfony Apps
PDF
Doctrine For Beginners
PDF
Using Objects to Organize your jQuery Code
PDF
Matters of State
PDF
Love and Loss: A Symfony Security Play
PDF
History of jQuery
PDF
Design how your objects talk through mocking
PPTX
Taming that client side mess with Backbone.js
PDF
The Origin of Lithium
PDF
How kris-writes-symfony-apps-london
PDF
The Zen of Lithium
PDF
How Kris Writes Symfony Apps
PDF
PHP 5.3 and Lithium: the most rad php framework
PDF
Javascript Frameworks for Joomla
KEY
Backbone js
PDF
Jqeury ajax plugins
KEY
Ruby/Rails
PDF
jQuery: Events, Animation, Ajax
Delivering a Responsive UI
Dojo Confessions
How Kris Writes Symfony Apps
Doctrine For Beginners
Using Objects to Organize your jQuery Code
Matters of State
Love and Loss: A Symfony Security Play
History of jQuery
Design how your objects talk through mocking
Taming that client side mess with Backbone.js
The Origin of Lithium
How kris-writes-symfony-apps-london
The Zen of Lithium
How Kris Writes Symfony Apps
PHP 5.3 and Lithium: the most rad php framework
Javascript Frameworks for Joomla
Backbone js
Jqeury ajax plugins
Ruby/Rails
jQuery: Events, Animation, Ajax
Ad

Similar to Jquery Fundamentals (20)

PDF
J query fundamentals
PPTX
jQuery
PPTX
Jqueryppt (1)
PDF
Jquery in-15-minutes1421
PPT
J query presentation
PPT
J query presentation
PDF
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
PPT
Jquery presentation
PPT
jQuery for beginners
PDF
jQuery in 15 minutes
PDF
Remy Sharp The DOM scripting toolkit jQuery
PDF
DOM Scripting Toolkit - jQuery
KEY
Week 4 - jQuery + Ajax
PPTX
Introduction to Jquery
PPTX
jQuery basics for Beginners
PDF
PDF
Lotusphere 2012 Speedgeeking - jQuery & Domino, a RAD Combination
PPTX
Getting started with jQuery
PPTX
jQuery
PPTX
Web technologies-course 11.pptx
J query fundamentals
jQuery
Jqueryppt (1)
Jquery in-15-minutes1421
J query presentation
J query presentation
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
Jquery presentation
jQuery for beginners
jQuery in 15 minutes
Remy Sharp The DOM scripting toolkit jQuery
DOM Scripting Toolkit - jQuery
Week 4 - jQuery + Ajax
Introduction to Jquery
jQuery basics for Beginners
Lotusphere 2012 Speedgeeking - jQuery & Domino, a RAD Combination
Getting started with jQuery
jQuery
Web technologies-course 11.pptx
Ad

More from Rebecca Murphey (8)

PDF
Getting Started with Mulberry
PDF
Introducing Mulberry
PDF
DojoConf: Building Large Apps
PDF
Lessons from-a-rewrite-gotham
PDF
Lessons from a Rewrite
PDF
Modern JavaScript
PDF
Cleaner, Leaner, Meaner: Refactoring your jQuery
PDF
The jQuery Divide
Getting Started with Mulberry
Introducing Mulberry
DojoConf: Building Large Apps
Lessons from-a-rewrite-gotham
Lessons from a Rewrite
Modern JavaScript
Cleaner, Leaner, Meaner: Refactoring your jQuery
The jQuery Divide

Recently uploaded (20)

PDF
Approach and Philosophy of On baking technology
PDF
Encapsulation theory and applications.pdf
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
Machine Learning_overview_presentation.pptx
PPTX
MYSQL Presentation for SQL database connectivity
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
Spectroscopy.pptx food analysis technology
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Empathic Computing: Creating Shared Understanding
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Encapsulation_ Review paper, used for researhc scholars
Approach and Philosophy of On baking technology
Encapsulation theory and applications.pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Spectral efficient network and resource selection model in 5G networks
NewMind AI Weekly Chronicles - August'25-Week II
Network Security Unit 5.pdf for BCA BBA.
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Machine Learning_overview_presentation.pptx
MYSQL Presentation for SQL database connectivity
A comparative analysis of optical character recognition models for extracting...
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
Spectroscopy.pptx food analysis technology
Reach Out and Touch Someone: Haptics and Empathic Computing
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Empathic Computing: Creating Shared Understanding
Programs and apps: productivity, graphics, security and other tools
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Encapsulation_ Review paper, used for researhc scholars

Jquery Fundamentals

  • 1. jQuery Fundamentals [email protected] @rmurphey on Twitter http://delicious.com/rdmey/jquery-class
  • 3. In the beginning, there was JavaScript http://www.flickr.com/photos/freeparking/476376873/
  • 4. Enter jQuery http://www.flickr.com/photos/timsnell/513274898/
  • 5. • Reads like what you mean • Simplifies cross-browser issues • Huge community & plugin ecosystem
  • 6. JavaScript 101 Because there’s lots of stuff you still need to know.
  • 8. Objects • Booleans, strings, numbers, arrays, and functions are all objects • Objects are a good way of organizing your code http://www.flickr.com/photos/oxido1180/2685774194/
  • 10. Booleans (true/false) • Lots of things are “truthy” • true • '0' (or any non-empty string) •1 (or any non-zero number) • any array, even empty ones • any object, even empty ones
  • 11. Booleans (true/false) • A few things are “falsy” • false •0 • undefined • null • '' (an empty, zero-length string) • NaN (“not a number,” the result of illegal math operations)
  • 15. function() { } a function
  • 16. Functions • Functions can take arguments, but they don't have to • Functionscan return anything, including other functions -- but they don't have to return anything • Functions can be assigned to variables • Functions can be passed to other functions by name, or as anonymous functions • Functions are normally called by putting () after their name
  • 17. Variable scope http://www.flickr.com/photos/8/12978541/
  • 18. Closures http://www.flickr.com/photos/gadl/272562772/
  • 20. In a nutshell • Get some elements • Do something to them http://www.flickr.com/photos/exfordy/1184487050/
  • 21. $()
  • 23. Chaining http://www.flickr.com/photos/itsgreg/315015695/
  • 24. Events http://docs.jquery.com/Events http://docs.jquery.com/Events/jQuery.Event
  • 25. $('a.foo').click(function(e) { e.preventDefault(); var $this = $(this); $this.css({ color : 'red' }); console.log('You clicked a link that points to ' + $this.attr('href')); }); when an a.foo link is clicked, prevent the default action, color the link red, and tell the user where the link pointed to
  • 26. CSS
  • 27. Manipulating http://www.flickr.com/photos/nnova/3373350948/
  • 28. Traversing http://www.flickr.com/photos/jenny-pics/3258777653/
  • 29. Effects http://www.flickr.com/photos/foxypar4/2153422313/
  • 30. XHR http://www.flickr.com/photos/timsnell/513274898/
  • 31. XHR basics • type: GET or POST • url: a fully qualified or relative URL • data: object or query string • dataType: text, html, xml, json, jsonp, script • callback: function to run when data is received
  • 32. POST or GET? • POST when you want to change something on the server • GET when you just want to get something from the server http://www.flickr.com/photos/14511253@N04/3298001461/
  • 33. POST or GET? • POST when you want to change something on the server • GET when you just want to get something from the server http://www.flickr.com/photos/14511253@N04/3298001461/
  • 34. More options with $.ajax() • async • error callback • timeout • cache • header modification • more ... http://www.flickr.com/photos/jeet_sen/1691759831/
  • 35. XHR events • ajaxStart • ajaxStop • ajaxComplete • ajaxError • ajaxSend • ajaxSuccess
  • 36. $('#loading') .ajaxStart(function() { $(this).show(); }) .ajaxStop(function() { $(this).hide(); });
  • 37. Utility Methods http://www.flickr.com/photos/geoftheref/2486112196/
  • 39. Plugins • Extend core jQuery • $.myPlugin() • Extend jQuery object methods • $('#foo') .myPlugin()
  • 40. Extending object methods • Chainability • Options & defaults • Callbacks
  • 41. jQuery.fn.myPlugin = function(options) { jQuery.fn.myPlugin.defaults = { // ... }; var settings = $.extend( jQuery.fn.myPlugin.defaults, options ); return this.each(function() { // ... }); }; basic concept of a jQuery object method
  • 42. jQuery.fn.myPlugin = function(options) { jQuery.fn.myPlugin.defaults = { // ... }; var settings = $.extend( jQuery.fn.myPlugin.defaults, options ); return this.each(function() { // ... }); }; naming the plugin
  • 43. jQuery.fn.myPlugin = function(options) { jQuery.fn.myPlugin.defaults = { // ... }; var settings = $.extend( jQuery.fn.myPlugin.defaults, options ); return this.each(function() { // ... }); }; setting the defaults for the plugin
  • 44. jQuery.fn.myPlugin = function(options) { jQuery.fn.myPlugin.defaults = { // ... }; var settings = $.extend( jQuery.fn.myPlugin.defaults, options ); return this.each(function() { // ... }); }; overriding the defaults with user-defined options
  • 45. jQuery.fn.myPlugin = function(options) { jQuery.fn.myPlugin.defaults = { // ... }; var settings = $.extend( jQuery.fn.myPlugin.defaults, options ); return this.each(function() { // ... }); }; enabling chaining
  • 46. jQuery.fn.hrefToContents = function(options) { return this.each(function() { $(this).text($(this).attr('href')); }); }; a plugin that requires iteration
  • 47. jQuery.fn.hoverClass = function(c) { return this.hover( function() { $(this).addClass(c); }, function() { $(this).removeClass(c); } ); }; a plugin that doesn't require iteration
  • 48. jQuery.fn.myPlugin = function(options) { jQuery.fn.myPlugin.defaults = { callback : function() { alert('done'); } }; var settings = $.extend( jQuery.fn.myPlugin.defaults, options ); this.each(function() { // ... }); jQuery.fn.myPlugin.defaults.callback(); return this; }; adding a callback