SlideShare a Scribd company logo
1 of 24
Download to read offline
Introducing jQuery
Grzegorz Ziolkowski
Agenda
●Getting started
●Using selectors and page traversing
●Handling events
●Effects
●DOM manipulation
●Mouse interaction and UI extensions
What jQuery does?
●Access parts of page (DOM traversing)
●Modify the appearance of a page
●Alter the content of a page
●Respond to a user’s interaction with a page
●Add animation to a page
●Retrieve information from a server without
refreshing a page (AJAX)
●Simplify common JavaScript tasks
Why jQuery works well?
●Leverage knowledge of CSS
●Support extension
●Abstract away browser quirks
●Always works with sets (implicit iteration)
●Allow multiple actions in one line
(chaining)
Our first jQuery Document
●Downloading jQuery: http://jquery.
com/download/
●Setting up the HTML document
●Writing the jQuery code
○finding the element
○adding new property
○executing the code
Handling events
Grzegorz Ziolkowski
Events - Performing task on page load
●Timing of code execution
●Document is completely downloaded to the
browser
○window.onload
●DOM is completely ready for use
○$( document ).ready()
●When you can use jQuery's .load()
○image gallery with many images
Events - Multiple scripts on one page
●Traditional mechanism for registering
event handlers through JavaScript
○<body onload="doStuff();">
○window.onload = doStuff; (more cleanly
separated from the markup)
●What if we want add second function
doOtherStuff()?
○window.onload = doOtherStuff - wrong
○$( document ).ready(); - each call adds the new
function to the internal queue of behaviours
Events - Simple events
●Shortcuts for code brevity
○$( document ).ready( function() {} );
○$().ready( function() {} );
○$( function() {} );
●Using .on() in examples
●Shorthand for native JavaScript events
●Compound event handlers
○.hover()
Events - The journey of an event
●Event occurs on a page - entire hierarchy
of DOM elements gets chance to handle it
○When the anchor is clicked, the <div>, <span>, and
<a> all should respond to the click
Events - Event capturing
●Allowing multiple elements to respond to
an event is called event capturing
○ The event is first given to the most all-encompassing
element, and then to more specific ones
Events - Event bubbling
●Opposite strategy is called event bubbling
○ The event gets sent to the most specific element, and after
element has opportunity to react, the event bubbles to
more general elements
Events - Implementation in browsers :)
●Different browser developers - different
models for event propagation
●DOM standard:
○event is captured from general to specific
○event bubbles back up to the top of the DOM tree
○event handlers can be registered for either part of the
process
●Always registers event handlers for the
bubbling phase of the model to provide
cross-browser consistency
Events - Side effects of event bubbling
●Event bubbling can cause unexpected
behavior when the wrong element
responds to a mouseover or mouseout
○when the user's mouse cursor exits <div> mouseout
isn't propagated to other elements
○when the cursor exits the <a> element, mouseout is
bubbled up to the parent elements (can cause
unexpected behaviors)
●The .hover() method is aware of this
problems
Events - Limiting and ending events
●We can access event object which is
passed to each event handler
●It provides information about event, such
as where the mouse was at the time
○$( '#elem' ).click( function( event ) {...} )
●Use event.target property to locate where
an event takes effect (part of DOM)
○if ( event.target == this ) { ... }
●.stopPropagation() method can eliminate
bubbling completely for the event
Events - Preventing default actions
●Default actions - submit button in form,
anchor with href - click event
●These actions occur nowhere in the normal
flow of event propagation so calling .
stopPropagation() will not help
●.preventDefault() stops the event in its
tracks before the default action is triggered
●We can stop both mechanisms simply
returning false in our event handler
Events - Removing an event handler
●We can use .off() method to remove the
handler(s) from element
●It takes an event type as its first argument,
and a function to remove as second
●Omitting the function name will remove all
functions
●Omitting the method name will remove all
events
Events - Simulating user interaction
●The .trigger() method allow us to execute
code that we have bound to an event
●Even if the normal circumstances of the
event are not occurring
●When triggering event that way event
propagation does not occur
●We have to perform trigger on element
where handlers were attached directly
●Shortcuts are allowed - click()
DOM manipulation
Grzegorz Ziolkowski
DOM - Manipulating attributes
●So far we have seen methods which allow
change element's class:
○.addClass()
○.removeClass()
○.toggleClass()
●We already know how to change element's
attributes and appearance:
○.attr()
○.removeAttr()
○.css()
●Using .each() method and iterator
DOM - New use of $() factory
●So far we've been using $() function to
access elements in a document to:
○attach or deattach effect
○attach or deattach event
○add, modify or delete property
●It also allows insert a set of HTML elements,
move part of HTML to another place, remove
selected code or even copy some part of page
DOM - Manipulation methods
●To insert new element(s) inside every
matched element, you can use:
○.append() or appendTo()
○.prepend() or .prependTo()
●To insert new element(s) adjacent to every matched
element, you can use:
○.after() or insertAfter()
○.before() or insertBefore()
DOM - Manipulation methods
●To insert new element(s) around every
matched element, you can use:
○.wrap()
●To replace every matched element with new
element(s) or text, you can use:
○.html()
○.text()
DOM - Manipulation methods
●To remove element(s) inside every
matched element, you can use:
○.empty()
●To remove every matched element and
descendants from the document without
actually deleting them, you can use
○.remove()

More Related Content

Viewers also liked (6)

jQuery 把玩 SVG 初體驗
jQuery 把玩 SVG 初體驗jQuery 把玩 SVG 初體驗
jQuery 把玩 SVG 初體驗
 
GSS Frontend Project Management
GSS Frontend Project ManagementGSS Frontend Project Management
GSS Frontend Project Management
 
漫談 CSS 架構方法 - 以 OOCSS, SMACSS, BEM 為例
漫談 CSS 架構方法 - 以 OOCSS, SMACSS, BEM 為例漫談 CSS 架構方法 - 以 OOCSS, SMACSS, BEM 為例
漫談 CSS 架構方法 - 以 OOCSS, SMACSS, BEM 為例
 
使用 TypeScript 駕馭 Web 世界的脫韁野馬:以 Angular 2 開發框架為例
使用 TypeScript 駕馭 Web 世界的脫韁野馬:以 Angular 2 開發框架為例使用 TypeScript 駕馭 Web 世界的脫韁野馬:以 Angular 2 開發框架為例
使用 TypeScript 駕馭 Web 世界的脫韁野馬:以 Angular 2 開發框架為例
 
AlphaGo in Depth
AlphaGo in Depth AlphaGo in Depth
AlphaGo in Depth
 
Vital UI kit
Vital UI kitVital UI kit
Vital UI kit
 

Similar to Introducing jQuery

ZUYU Design Quick Review
ZUYU Design Quick ReviewZUYU Design Quick Review
ZUYU Design Quick Review
mamahow
 
13. session 13 introduction to dhtml
13. session 13   introduction to dhtml13. session 13   introduction to dhtml
13. session 13 introduction to dhtml
Phúc Đỗ
 
Mooscon 2013 cebit - google integration in android apps (1)
Mooscon 2013   cebit - google integration in android apps (1)Mooscon 2013   cebit - google integration in android apps (1)
Mooscon 2013 cebit - google integration in android apps (1)
Heinrich Seeger
 
Java2day 2013 : Modern workflows for javascript integration
Java2day 2013 : Modern workflows for javascript integrationJava2day 2013 : Modern workflows for javascript integration
Java2day 2013 : Modern workflows for javascript integration
Mite Mitreski
 

Similar to Introducing jQuery (20)

Javascript Browser Events.pdf
Javascript Browser Events.pdfJavascript Browser Events.pdf
Javascript Browser Events.pdf
 
ZUYU Design Quick Review
ZUYU Design Quick ReviewZUYU Design Quick Review
ZUYU Design Quick Review
 
Air Drag And Drop
Air Drag And DropAir Drag And Drop
Air Drag And Drop
 
13. session 13 introduction to dhtml
13. session 13   introduction to dhtml13. session 13   introduction to dhtml
13. session 13 introduction to dhtml
 
B2. activity and intent
B2. activity and intentB2. activity and intent
B2. activity and intent
 
FirefoxOS Window Management
FirefoxOS Window ManagementFirefoxOS Window Management
FirefoxOS Window Management
 
JavascriptMVC: Another choice of web framework
JavascriptMVC: Another choice of web frameworkJavascriptMVC: Another choice of web framework
JavascriptMVC: Another choice of web framework
 
Events.pdf
Events.pdfEvents.pdf
Events.pdf
 
Xamarin.android memory management gotchas
Xamarin.android memory management gotchasXamarin.android memory management gotchas
Xamarin.android memory management gotchas
 
Riacon swiz
Riacon swizRiacon swiz
Riacon swiz
 
jQuery Conference Austin Sept 2013
jQuery Conference Austin Sept 2013jQuery Conference Austin Sept 2013
jQuery Conference Austin Sept 2013
 
Mooscon 2013 cebit - google integration in android apps (1)
Mooscon 2013   cebit - google integration in android apps (1)Mooscon 2013   cebit - google integration in android apps (1)
Mooscon 2013 cebit - google integration in android apps (1)
 
Java2day 2013 : Modern workflows for javascript integration
Java2day 2013 : Modern workflows for javascript integrationJava2day 2013 : Modern workflows for javascript integration
Java2day 2013 : Modern workflows for javascript integration
 
Life Cycle hooks in VueJs
Life Cycle hooks in VueJsLife Cycle hooks in VueJs
Life Cycle hooks in VueJs
 
Presentation on Android application life cycle and saved instancestate
Presentation on Android application life cycle and saved instancestatePresentation on Android application life cycle and saved instancestate
Presentation on Android application life cycle and saved instancestate
 
(Js) Export your own WebGL Viewer
(Js) Export your own WebGL Viewer(Js) Export your own WebGL Viewer
(Js) Export your own WebGL Viewer
 
Event Programming JavaScript
Event Programming JavaScriptEvent Programming JavaScript
Event Programming JavaScript
 
Unit3.pptx
Unit3.pptxUnit3.pptx
Unit3.pptx
 
javascript Event Handling and introduction to event.ppt
javascript Event Handling and introduction to event.pptjavascript Event Handling and introduction to event.ppt
javascript Event Handling and introduction to event.ppt
 
Griffon demo
Griffon demoGriffon demo
Griffon demo
 

Recently uploaded

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
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
 
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...
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
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
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
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
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 

Introducing jQuery

  • 2. Agenda ●Getting started ●Using selectors and page traversing ●Handling events ●Effects ●DOM manipulation ●Mouse interaction and UI extensions
  • 3. What jQuery does? ●Access parts of page (DOM traversing) ●Modify the appearance of a page ●Alter the content of a page ●Respond to a user’s interaction with a page ●Add animation to a page ●Retrieve information from a server without refreshing a page (AJAX) ●Simplify common JavaScript tasks
  • 4. Why jQuery works well? ●Leverage knowledge of CSS ●Support extension ●Abstract away browser quirks ●Always works with sets (implicit iteration) ●Allow multiple actions in one line (chaining)
  • 5. Our first jQuery Document ●Downloading jQuery: http://jquery. com/download/ ●Setting up the HTML document ●Writing the jQuery code ○finding the element ○adding new property ○executing the code
  • 7. Events - Performing task on page load ●Timing of code execution ●Document is completely downloaded to the browser ○window.onload ●DOM is completely ready for use ○$( document ).ready() ●When you can use jQuery's .load() ○image gallery with many images
  • 8. Events - Multiple scripts on one page ●Traditional mechanism for registering event handlers through JavaScript ○<body onload="doStuff();"> ○window.onload = doStuff; (more cleanly separated from the markup) ●What if we want add second function doOtherStuff()? ○window.onload = doOtherStuff - wrong ○$( document ).ready(); - each call adds the new function to the internal queue of behaviours
  • 9. Events - Simple events ●Shortcuts for code brevity ○$( document ).ready( function() {} ); ○$().ready( function() {} ); ○$( function() {} ); ●Using .on() in examples ●Shorthand for native JavaScript events ●Compound event handlers ○.hover()
  • 10. Events - The journey of an event ●Event occurs on a page - entire hierarchy of DOM elements gets chance to handle it ○When the anchor is clicked, the <div>, <span>, and <a> all should respond to the click
  • 11. Events - Event capturing ●Allowing multiple elements to respond to an event is called event capturing ○ The event is first given to the most all-encompassing element, and then to more specific ones
  • 12. Events - Event bubbling ●Opposite strategy is called event bubbling ○ The event gets sent to the most specific element, and after element has opportunity to react, the event bubbles to more general elements
  • 13. Events - Implementation in browsers :) ●Different browser developers - different models for event propagation ●DOM standard: ○event is captured from general to specific ○event bubbles back up to the top of the DOM tree ○event handlers can be registered for either part of the process ●Always registers event handlers for the bubbling phase of the model to provide cross-browser consistency
  • 14. Events - Side effects of event bubbling ●Event bubbling can cause unexpected behavior when the wrong element responds to a mouseover or mouseout ○when the user's mouse cursor exits <div> mouseout isn't propagated to other elements ○when the cursor exits the <a> element, mouseout is bubbled up to the parent elements (can cause unexpected behaviors) ●The .hover() method is aware of this problems
  • 15. Events - Limiting and ending events ●We can access event object which is passed to each event handler ●It provides information about event, such as where the mouse was at the time ○$( '#elem' ).click( function( event ) {...} ) ●Use event.target property to locate where an event takes effect (part of DOM) ○if ( event.target == this ) { ... } ●.stopPropagation() method can eliminate bubbling completely for the event
  • 16. Events - Preventing default actions ●Default actions - submit button in form, anchor with href - click event ●These actions occur nowhere in the normal flow of event propagation so calling . stopPropagation() will not help ●.preventDefault() stops the event in its tracks before the default action is triggered ●We can stop both mechanisms simply returning false in our event handler
  • 17. Events - Removing an event handler ●We can use .off() method to remove the handler(s) from element ●It takes an event type as its first argument, and a function to remove as second ●Omitting the function name will remove all functions ●Omitting the method name will remove all events
  • 18. Events - Simulating user interaction ●The .trigger() method allow us to execute code that we have bound to an event ●Even if the normal circumstances of the event are not occurring ●When triggering event that way event propagation does not occur ●We have to perform trigger on element where handlers were attached directly ●Shortcuts are allowed - click()
  • 20. DOM - Manipulating attributes ●So far we have seen methods which allow change element's class: ○.addClass() ○.removeClass() ○.toggleClass() ●We already know how to change element's attributes and appearance: ○.attr() ○.removeAttr() ○.css() ●Using .each() method and iterator
  • 21. DOM - New use of $() factory ●So far we've been using $() function to access elements in a document to: ○attach or deattach effect ○attach or deattach event ○add, modify or delete property ●It also allows insert a set of HTML elements, move part of HTML to another place, remove selected code or even copy some part of page
  • 22. DOM - Manipulation methods ●To insert new element(s) inside every matched element, you can use: ○.append() or appendTo() ○.prepend() or .prependTo() ●To insert new element(s) adjacent to every matched element, you can use: ○.after() or insertAfter() ○.before() or insertBefore()
  • 23. DOM - Manipulation methods ●To insert new element(s) around every matched element, you can use: ○.wrap() ●To replace every matched element with new element(s) or text, you can use: ○.html() ○.text()
  • 24. DOM - Manipulation methods ●To remove element(s) inside every matched element, you can use: ○.empty() ●To remove every matched element and descendants from the document without actually deleting them, you can use ○.remove()