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
 
jQuery-1-Ajax
jQuery-1-AjaxjQuery-1-Ajax
jQuery-1-Ajax
 
<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
 
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

Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
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
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
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
 
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
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
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
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
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
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
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
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024SynarionITSolutions
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 

Recently uploaded (20)

Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
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
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
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
 
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
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
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
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
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...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
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...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 

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