SlideShare a Scribd company logo
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

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

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

Javascript Browser Events.pdf
Javascript Browser Events.pdfJavascript Browser Events.pdf
Javascript Browser Events.pdf
ShubhamChaurasia88
 
ZUYU Design Quick Review
ZUYU Design Quick ReviewZUYU Design Quick Review
ZUYU Design Quick Review
mamahow
 
Air Drag And Drop
Air Drag And DropAir Drag And Drop
Air Drag And Drop
michael.labriola
 
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 Đỗ
 
B2. activity and intent
B2. activity and intentB2. activity and intent
B2. activity and intent
PERKYTORIALS
 
FirefoxOS Window Management
FirefoxOS Window ManagementFirefoxOS Window Management
FirefoxOS Window Management
Alive Kuo
 
JavascriptMVC: Another choice of web framework
JavascriptMVC: Another choice of web frameworkJavascriptMVC: Another choice of web framework
JavascriptMVC: Another choice of web framework
Alive Kuo
 
Events.pdf
Events.pdfEvents.pdf
Events.pdf
stephanedjeukam1
 
Xamarin.android memory management gotchas
Xamarin.android memory management gotchasXamarin.android memory management gotchas
Xamarin.android memory management gotchas
Alec Tucker
 
Riacon swiz
Riacon swizRiacon swiz
Riacon swiz
ntunney
 
jQuery Conference Austin Sept 2013
jQuery Conference Austin Sept 2013jQuery Conference Austin Sept 2013
jQuery Conference Austin Sept 2013
dmethvin
 
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
 
Life Cycle hooks in VueJs
Life Cycle hooks in VueJsLife Cycle hooks in VueJs
Life Cycle hooks in VueJs
Squash Apps Pvt Ltd
 
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
Osahon Gino Ediagbonya
 
(Js) Export your own WebGL Viewer
(Js) Export your own WebGL Viewer(Js) Export your own WebGL Viewer
(Js) Export your own WebGL Viewer
JooinK
 
Event Programming JavaScript
Event Programming JavaScriptEvent Programming JavaScript
Event Programming JavaScript
MuhammadRehan856177
 
Unit3.pptx
Unit3.pptxUnit3.pptx
Unit3.pptx
AnamikaRai59
 
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
Lalith86
 
Griffon demo
Griffon demoGriffon demo
Griffon demo
James Kirkbride
 

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

OpenID AuthZEN Interop Read Out - Authorization
OpenID AuthZEN Interop Read Out - AuthorizationOpenID AuthZEN Interop Read Out - Authorization
OpenID AuthZEN Interop Read Out - Authorization
David Brossard
 
Project Management Semester Long Project - Acuity
Project Management Semester Long Project - AcuityProject Management Semester Long Project - Acuity
Project Management Semester Long Project - Acuity
jpupo2018
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
Zilliz
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
panagenda
 
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Speck&Tech
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
Ivanti
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
Jakub Marek
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
MichaelKnudsen27
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
danishmna97
 
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdfMonitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Tosin Akinosho
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
innovationoecd
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
panagenda
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Malak Abu Hammad
 
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Jeffrey Haguewood
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
Chart Kalyan
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
akankshawande
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
Tomaz Bratanic
 
How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
Daiki Mogmet Ito
 

Recently uploaded (20)

OpenID AuthZEN Interop Read Out - Authorization
OpenID AuthZEN Interop Read Out - AuthorizationOpenID AuthZEN Interop Read Out - Authorization
OpenID AuthZEN Interop Read Out - Authorization
 
Project Management Semester Long Project - Acuity
Project Management Semester Long Project - AcuityProject Management Semester Long Project - Acuity
Project Management Semester Long Project - Acuity
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
 
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
 
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdfMonitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdf
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
 
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
 
How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
 

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()