SlideShare a Scribd company logo
1 of 20
jQuery Jon Erickson @jonerickson http://jonerickson.me/ http://jonerickson.me/jquery/
What is jQuery? JavaScript framework that makes your life easier Selectors & Chaining DOM Traversing DOM Manipulation Event Handling Animations/Effects AJAX Built-in Utilities Extensible Tons of plug-ins built by the community for our use
Why jQuery? I’m not an expert at JavaScript The people who created jQuery are! Do not need to worry about cross-browser issues (all abstracted) Light Weight (19 kb compressed) DRY – Simple plug-in model allows for code reuse Animations/Effects are simple Community support is growing rapidly Microsoft adoption (included in MVC template) Tutorials, plug-ins, etc. Client-side scripting is fun with jQuery and breathes some new life into your applications Focus more on the code that solves your problem Amazon, BBC, Dell, Google, IBM, NBC, Netflix, Twitter
Sample jQuery Our DOM Include jQuery The ‘ready event’ – Binds a function to be executed whenever the DOM is ready Select the ‘toggleContent’ DOM element and bind a click event handler to it. Select the ‘content’ DOM element and set the text to ‘Hello World!’ Prevent the default behavior of the anchor tag by returning false
Dollar Function $(); JavaScript identifiers can start with $ The jQuery framework automatically assigns ‘$’ to the ‘jQuery’ function $ === jQuerytrue $(selector) is same as jQuery(selector) Can use utility function to unassign $ $.noConflict(); $ === jQuery;	false Can reassign jQuery to another variable $j = $.noConflict();	 $j === jQuery;	true $j === $;		false $ === jQuery;	false
Document Ready Event $(document).ready(fn); The bound function will be called the instant the DOM is ready to be read and manipulated. As many as you want on the page. Executed in the order they were added. There is a shortcut: $(fn);
Selectors CSS3 Selectors
Filters $(‘div:empty’), $(‘:empty’), $(‘div:not(:empty)’) Custom Filter Used
Chaining Most jQuery methods return another jQuery object (usually the same collection), which means you can chain method calls together with a fluent like syntax Some methods that stop a chain, these methods return a value from the jQuery object 	.css(name), .text(), .html(), .val(), .height(), .width(), .is(‘:visible’) Some methods will return a different jQuery collection, you can revert to the previous collection by calling .end();
Attributes & Classes Getters & Setters for attr, html, text, val Getter (returns String – breaks chain) var text = $(‘#myDiv’).text(); Setter (returns jQuery) $(‘#myDiv’).text(‘Hello World!’); text() escapes html() val() used with inputs attr() can take JSON Add, Remove for Attributes & Classes .removeAttr(‘someAttr’); .addClass(‘someClass’); .removeClass(‘someClass’); .toggleClass(‘someClass’);
Traversing Family parent, parents, siblings, children Proximity closest, next, prev, nextAll, prevAll Searching find
Events .bind(type, data, fn) ,[object Object],.one(type, data, fn) ,[object Object],.trigger(event, data) ,[object Object],.unbind(type, fn) ,[object Object],.live(type, fn) ,[object Object],.die(type, fn) ,[object Object],.hover(fnOver, fnOut) ,[object Object],.toggle(fn1, fn2, fn3, fnN, …) ,[object Object],[object Object]
Manipulation Inserting Inside append, prepend appendTo, prependTo Inserting Outside after, before insertAfter, insertBefore Inserting Around wrapInner wrap wrapAll Replacing replaceWith, replaceAll Removing empty, remove Copying clone
Effects Basics show, hide, toggle Sliding slideUp, slideDown, slideToggle Fading fadeIn, fadeOut, fadeTo (opacity 0-1) Custom animate, stop
Plug-ins Extremely Simple – Promotes code reuse and DRY principle $.fn.MyPlugin = function(params) {}; Return a jQuery object to prevent from “breaking the chain” Unless you are returning a specific value Best Practice is to wrap the plug-in declaration within an anonymous JavaScript function in order to prevent collisions with the use of $ Within the plug-in ‘this’ refers to the jQuery object
AJAX Helpers load, get, getJSON, getScript, post (all use $.ajax behind the scenes) $.ajaxSetup(options) can be used to globally set the default options for all AJAX request. You can still override these within each AJAX request object. Local Events – subscribed to within AJAX request object Global Events – broadcast to all elements in the DOM Can be disabled within the AJAX request object’s ‘global’ property .bind(‘ajaxStart’, fn) or .ajaxStart(fn) G | ajaxStart L | beforeSend G | ajaxSend L | success G | ajaxSuccess L | error G | ajaxError L | complete G | ajaxComplete G | ajaxStop
Utility Functions Array and Object Operations $.each(object, callback) – Callback function will run for each item in the object. The each method is meant to be an immutable iterator and returns the original array. $.map(array, callback) – Callback function will run for each item in the array. The map method can be used as an iterator, but is designed to be used for manipulation of the supplied array and returns a new array. $.merge(first, second) – Merges the second array into the first array. $.unique(array) – Removes duplicate elements (only works on arrays of DOM elements). $.extend(object) – Add functions into the jQuery namespace. $.extend(deep, target, object1, objectN) – Merge values from objects 1 to N into target object. Deep is an optional boolean that if true, tells jQuery to do a deep copy. $.grep(array, callback, invert) – Iterates through array, and returns a new array with values from the original array that satisfy the criteria specified in the callback. $.inArray(value, array) – Gets the index of the value in the array (-1 if not found). String Operations $.trim(str) – Removes whitespace from the given string. Test Operations $.isArray(obj) – Determines if the object is an array. $.isFunction(obj) – Determines if the object is a function.
jQuery Data Can store data on one or more jQuery elements .data(name, value) value is an object Retrieves data from the first element in the jQuery object .data(name)
Resources jQuery Main http://jquery.com http://docs.jquery.com/Downloading_jQuery jQuery API Documentation http://api.jquery.com http://docs.jquery.com jQuery UI http://jqueryui.com http://jqueryui.com/themeroller/ jQuery Blog http://blog.jquery.com/ Around The Web http://stackoverflow.com http://www.nettuts.com http://www.smashingmagazine.com Tools Visual Studio JavaScript Intellisense Support (KB958502) http://getfirebug.com/ (Firebug Firefox Plug-in) http://jsbin.com/  (JS Bin - Collaborative JavaScript Debugging) Twitter @jquery, @jqueryui, @jresig, @rem, @codylindley , @reybango, @elijahmanor, @jamespadolsey, @brandonaaron, @malsup, @pbakaus, @rworth
jQuery

More Related Content

What's hot

JavaScript Fundamentals with Angular and Lodash
JavaScript Fundamentals with Angular and LodashJavaScript Fundamentals with Angular and Lodash
JavaScript Fundamentals with Angular and LodashBret Little
 
Chaining and function composition with lodash / underscore
Chaining and function composition with lodash / underscoreChaining and function composition with lodash / underscore
Chaining and function composition with lodash / underscoreNicolas Carlo
 
Chapter 11.4
Chapter 11.4Chapter 11.4
Chapter 11.4sotlsoc
 
Wicket KT part 2
Wicket KT part 2Wicket KT part 2
Wicket KT part 2stuq
 
jQuery 1.7 visual cheat sheet
jQuery 1.7 visual cheat sheetjQuery 1.7 visual cheat sheet
jQuery 1.7 visual cheat sheetJiby John
 
Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)Jonas Bonér
 
ハイブリッド言語Scalaを使う
ハイブリッド言語Scalaを使うハイブリッド言語Scalaを使う
ハイブリッド言語Scalaを使うbpstudy
 

What's hot (9)

JavaScript Fundamentals with Angular and Lodash
JavaScript Fundamentals with Angular and LodashJavaScript Fundamentals with Angular and Lodash
JavaScript Fundamentals with Angular and Lodash
 
Chaining and function composition with lodash / underscore
Chaining and function composition with lodash / underscoreChaining and function composition with lodash / underscore
Chaining and function composition with lodash / underscore
 
Chapter 11.4
Chapter 11.4Chapter 11.4
Chapter 11.4
 
Wicket KT part 2
Wicket KT part 2Wicket KT part 2
Wicket KT part 2
 
Scala in practice
Scala in practiceScala in practice
Scala in practice
 
Introduction to-scala
Introduction to-scalaIntroduction to-scala
Introduction to-scala
 
jQuery 1.7 visual cheat sheet
jQuery 1.7 visual cheat sheetjQuery 1.7 visual cheat sheet
jQuery 1.7 visual cheat sheet
 
Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)
 
ハイブリッド言語Scalaを使う
ハイブリッド言語Scalaを使うハイブリッド言語Scalaを使う
ハイブリッド言語Scalaを使う
 

Viewers also liked

Viewers also liked (6)

Kaleehume 3222
Kaleehume 3222Kaleehume 3222
Kaleehume 3222
 
Livejournal
LivejournalLivejournal
Livejournal
 
Sawwan desire 1_3
Sawwan desire 1_3Sawwan desire 1_3
Sawwan desire 1_3
 
Sawwan desire 1-3
Sawwan desire 1-3Sawwan desire 1-3
Sawwan desire 1-3
 
Livejournal
LivejournalLivejournal
Livejournal
 
Mueller n 1_22_2-1
Mueller n 1_22_2-1Mueller n 1_22_2-1
Mueller n 1_22_2-1
 

Similar to jQuery

J Query The Write Less Do More Javascript Library
J Query   The Write Less Do More Javascript LibraryJ Query   The Write Less Do More Javascript Library
J Query The Write Less Do More Javascript Libraryrsnarayanan
 
A to Z about JQuery - Become Newbie to Expert Java Developer
A to Z about JQuery - Become Newbie to Expert Java DeveloperA to Z about JQuery - Become Newbie to Expert Java Developer
A to Z about JQuery - Become Newbie to Expert Java DeveloperManoj Bhuva
 
JQuery New Evolution
JQuery New EvolutionJQuery New Evolution
JQuery New EvolutionAllan Huang
 
jQuery Rescue Adventure
jQuery Rescue AdventurejQuery Rescue Adventure
jQuery Rescue AdventureAllegient
 
presentation_jquery_ppt.pptx
presentation_jquery_ppt.pptxpresentation_jquery_ppt.pptx
presentation_jquery_ppt.pptxazz71
 
A Rich Web experience with jQuery, Ajax and .NET
A Rich Web experience with jQuery, Ajax and .NETA Rich Web experience with jQuery, Ajax and .NET
A Rich Web experience with jQuery, Ajax and .NETJames Johnson
 
Microsoft PowerPoint - <b>jQuery</b>-1-Ajax.pptx
Microsoft PowerPoint - <b>jQuery</b>-1-Ajax.pptxMicrosoft PowerPoint - <b>jQuery</b>-1-Ajax.pptx
Microsoft PowerPoint - <b>jQuery</b>-1-Ajax.pptxtutorialsruby
 
<img src="../i/r_14.png" />
<img src="../i/r_14.png" /><img src="../i/r_14.png" />
<img src="../i/r_14.png" />tutorialsruby
 
Learning jQuery made exciting in an interactive session by one of our team me...
Learning jQuery made exciting in an interactive session by one of our team me...Learning jQuery made exciting in an interactive session by one of our team me...
Learning jQuery made exciting in an interactive session by one of our team me...Thinqloud
 
Unit 1 - What is jQuery_Why jQuery_Syntax_Selectors.pdf
Unit 1 - What is jQuery_Why jQuery_Syntax_Selectors.pdfUnit 1 - What is jQuery_Why jQuery_Syntax_Selectors.pdf
Unit 1 - What is jQuery_Why jQuery_Syntax_Selectors.pdfRAVALCHIRAG1
 
A Rich Web Experience with jQuery, Ajax and .NET
A Rich Web Experience with jQuery, Ajax and .NETA Rich Web Experience with jQuery, Ajax and .NET
A Rich Web Experience with jQuery, Ajax and .NETJames Johnson
 
Learning jQuery in 30 minutes
Learning jQuery in 30 minutesLearning jQuery in 30 minutes
Learning jQuery in 30 minutesSimon Willison
 

Similar to jQuery (20)

jQuery
jQueryjQuery
jQuery
 
J Query The Write Less Do More Javascript Library
J Query   The Write Less Do More Javascript LibraryJ Query   The Write Less Do More Javascript Library
J Query The Write Less Do More Javascript Library
 
A to Z about JQuery - Become Newbie to Expert Java Developer
A to Z about JQuery - Become Newbie to Expert Java DeveloperA to Z about JQuery - Become Newbie to Expert Java Developer
A to Z about JQuery - Become Newbie to Expert Java Developer
 
JQuery New Evolution
JQuery New EvolutionJQuery New Evolution
JQuery New Evolution
 
jQuery
jQueryjQuery
jQuery
 
jQuery Rescue Adventure
jQuery Rescue AdventurejQuery Rescue Adventure
jQuery Rescue Adventure
 
presentation_jquery_ppt.pptx
presentation_jquery_ppt.pptxpresentation_jquery_ppt.pptx
presentation_jquery_ppt.pptx
 
A Rich Web experience with jQuery, Ajax and .NET
A Rich Web experience with jQuery, Ajax and .NETA Rich Web experience with jQuery, Ajax and .NET
A Rich Web experience with jQuery, Ajax and .NET
 
Microsoft PowerPoint - <b>jQuery</b>-1-Ajax.pptx
Microsoft PowerPoint - <b>jQuery</b>-1-Ajax.pptxMicrosoft PowerPoint - <b>jQuery</b>-1-Ajax.pptx
Microsoft PowerPoint - <b>jQuery</b>-1-Ajax.pptx
 
<img src="../i/r_14.png" />
<img src="../i/r_14.png" /><img src="../i/r_14.png" />
<img src="../i/r_14.png" />
 
jQuery-1-Ajax
jQuery-1-AjaxjQuery-1-Ajax
jQuery-1-Ajax
 
jQuery-1-Ajax
jQuery-1-AjaxjQuery-1-Ajax
jQuery-1-Ajax
 
droidparts
droidpartsdroidparts
droidparts
 
Learning jQuery made exciting in an interactive session by one of our team me...
Learning jQuery made exciting in an interactive session by one of our team me...Learning jQuery made exciting in an interactive session by one of our team me...
Learning jQuery made exciting in an interactive session by one of our team me...
 
Unit 1 - What is jQuery_Why jQuery_Syntax_Selectors.pdf
Unit 1 - What is jQuery_Why jQuery_Syntax_Selectors.pdfUnit 1 - What is jQuery_Why jQuery_Syntax_Selectors.pdf
Unit 1 - What is jQuery_Why jQuery_Syntax_Selectors.pdf
 
J query training
J query trainingJ query training
J query training
 
A Rich Web Experience with jQuery, Ajax and .NET
A Rich Web Experience with jQuery, Ajax and .NETA Rich Web Experience with jQuery, Ajax and .NET
A Rich Web Experience with jQuery, Ajax and .NET
 
Introduction to jQuery
Introduction to jQueryIntroduction to jQuery
Introduction to jQuery
 
Learning jQuery in 30 minutes
Learning jQuery in 30 minutesLearning jQuery in 30 minutes
Learning jQuery in 30 minutes
 
Ruby tricks2
Ruby tricks2Ruby tricks2
Ruby tricks2
 

Recently uploaded

The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 

Recently uploaded (20)

The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 

jQuery

  • 1. jQuery Jon Erickson @jonerickson http://jonerickson.me/ http://jonerickson.me/jquery/
  • 2. What is jQuery? JavaScript framework that makes your life easier Selectors & Chaining DOM Traversing DOM Manipulation Event Handling Animations/Effects AJAX Built-in Utilities Extensible Tons of plug-ins built by the community for our use
  • 3. Why jQuery? I’m not an expert at JavaScript The people who created jQuery are! Do not need to worry about cross-browser issues (all abstracted) Light Weight (19 kb compressed) DRY – Simple plug-in model allows for code reuse Animations/Effects are simple Community support is growing rapidly Microsoft adoption (included in MVC template) Tutorials, plug-ins, etc. Client-side scripting is fun with jQuery and breathes some new life into your applications Focus more on the code that solves your problem Amazon, BBC, Dell, Google, IBM, NBC, Netflix, Twitter
  • 4. Sample jQuery Our DOM Include jQuery The ‘ready event’ – Binds a function to be executed whenever the DOM is ready Select the ‘toggleContent’ DOM element and bind a click event handler to it. Select the ‘content’ DOM element and set the text to ‘Hello World!’ Prevent the default behavior of the anchor tag by returning false
  • 5. Dollar Function $(); JavaScript identifiers can start with $ The jQuery framework automatically assigns ‘$’ to the ‘jQuery’ function $ === jQuerytrue $(selector) is same as jQuery(selector) Can use utility function to unassign $ $.noConflict(); $ === jQuery; false Can reassign jQuery to another variable $j = $.noConflict(); $j === jQuery; true $j === $; false $ === jQuery; false
  • 6. Document Ready Event $(document).ready(fn); The bound function will be called the instant the DOM is ready to be read and manipulated. As many as you want on the page. Executed in the order they were added. There is a shortcut: $(fn);
  • 8. Filters $(‘div:empty’), $(‘:empty’), $(‘div:not(:empty)’) Custom Filter Used
  • 9. Chaining Most jQuery methods return another jQuery object (usually the same collection), which means you can chain method calls together with a fluent like syntax Some methods that stop a chain, these methods return a value from the jQuery object .css(name), .text(), .html(), .val(), .height(), .width(), .is(‘:visible’) Some methods will return a different jQuery collection, you can revert to the previous collection by calling .end();
  • 10. Attributes & Classes Getters & Setters for attr, html, text, val Getter (returns String – breaks chain) var text = $(‘#myDiv’).text(); Setter (returns jQuery) $(‘#myDiv’).text(‘Hello World!’); text() escapes html() val() used with inputs attr() can take JSON Add, Remove for Attributes & Classes .removeAttr(‘someAttr’); .addClass(‘someClass’); .removeClass(‘someClass’); .toggleClass(‘someClass’);
  • 11. Traversing Family parent, parents, siblings, children Proximity closest, next, prev, nextAll, prevAll Searching find
  • 12.
  • 13. Manipulation Inserting Inside append, prepend appendTo, prependTo Inserting Outside after, before insertAfter, insertBefore Inserting Around wrapInner wrap wrapAll Replacing replaceWith, replaceAll Removing empty, remove Copying clone
  • 14. Effects Basics show, hide, toggle Sliding slideUp, slideDown, slideToggle Fading fadeIn, fadeOut, fadeTo (opacity 0-1) Custom animate, stop
  • 15. Plug-ins Extremely Simple – Promotes code reuse and DRY principle $.fn.MyPlugin = function(params) {}; Return a jQuery object to prevent from “breaking the chain” Unless you are returning a specific value Best Practice is to wrap the plug-in declaration within an anonymous JavaScript function in order to prevent collisions with the use of $ Within the plug-in ‘this’ refers to the jQuery object
  • 16. AJAX Helpers load, get, getJSON, getScript, post (all use $.ajax behind the scenes) $.ajaxSetup(options) can be used to globally set the default options for all AJAX request. You can still override these within each AJAX request object. Local Events – subscribed to within AJAX request object Global Events – broadcast to all elements in the DOM Can be disabled within the AJAX request object’s ‘global’ property .bind(‘ajaxStart’, fn) or .ajaxStart(fn) G | ajaxStart L | beforeSend G | ajaxSend L | success G | ajaxSuccess L | error G | ajaxError L | complete G | ajaxComplete G | ajaxStop
  • 17. Utility Functions Array and Object Operations $.each(object, callback) – Callback function will run for each item in the object. The each method is meant to be an immutable iterator and returns the original array. $.map(array, callback) – Callback function will run for each item in the array. The map method can be used as an iterator, but is designed to be used for manipulation of the supplied array and returns a new array. $.merge(first, second) – Merges the second array into the first array. $.unique(array) – Removes duplicate elements (only works on arrays of DOM elements). $.extend(object) – Add functions into the jQuery namespace. $.extend(deep, target, object1, objectN) – Merge values from objects 1 to N into target object. Deep is an optional boolean that if true, tells jQuery to do a deep copy. $.grep(array, callback, invert) – Iterates through array, and returns a new array with values from the original array that satisfy the criteria specified in the callback. $.inArray(value, array) – Gets the index of the value in the array (-1 if not found). String Operations $.trim(str) – Removes whitespace from the given string. Test Operations $.isArray(obj) – Determines if the object is an array. $.isFunction(obj) – Determines if the object is a function.
  • 18. jQuery Data Can store data on one or more jQuery elements .data(name, value) value is an object Retrieves data from the first element in the jQuery object .data(name)
  • 19. Resources jQuery Main http://jquery.com http://docs.jquery.com/Downloading_jQuery jQuery API Documentation http://api.jquery.com http://docs.jquery.com jQuery UI http://jqueryui.com http://jqueryui.com/themeroller/ jQuery Blog http://blog.jquery.com/ Around The Web http://stackoverflow.com http://www.nettuts.com http://www.smashingmagazine.com Tools Visual Studio JavaScript Intellisense Support (KB958502) http://getfirebug.com/ (Firebug Firefox Plug-in) http://jsbin.com/ (JS Bin - Collaborative JavaScript Debugging) Twitter @jquery, @jqueryui, @jresig, @rem, @codylindley , @reybango, @elijahmanor, @jamespadolsey, @brandonaaron, @malsup, @pbakaus, @rworth

Editor's Notes

  1. Demo
  2. Demo
  3. Demo
  4. Demo
  5. Demo
  6. Demo
  7. Demo
  8. Demo