SlideShare a Scribd company logo
jQuery Fundamentals, DOM, Events, AJAX, UI Doncho Minkov Telerik Mobile Development Course mobiledevcourse.telerik.com Technical Trainer http://www.minkov.it
Table of Contents jQuery Fundamentals Selection and DOM Manipulation Events and Chaining AJAX jQuery AJAX Methods Executing AJAX Requests jQuery UI jQuery Widgets Implementing Drag and Drop
What is jQuery? The world’s most popular JavaScript library
What is jQuery? jQuery is a cross-browser JavaScript library  Designed to simplify the client-side scripting of HTML The most popular JavaScript library in use today Free, open source software jQuery's syntax is designed to make it easier to Navigate a document and select DOM elements  Create animations Handle events Develop AJAX applications
What is jQuery? (2) jQuery also provides capabilities for developers to create plugins for Low-level interaction and animation Advanced effects and high-level, theme-able widgets Creation of powerful and dynamic web pages Microsoft adopted jQuery within Visual Studio Uses in Microsoft's ASP.NET AJAX Framework and ASP.NET MVC Framework
Why jQuery is So Popular? Easy to learn  Fluent programming style Easy to extend You create new jQuery plugins by creating new JavaScript functions Powerful DOM Selection  Powered by CSS 3.0 Lightweight Community Support  Large community of developers and geeks
How to Add jQuery to a Web Site? Download jQuery files from http://www.jquery.com Self hosted You can choose to self host the  .js  file E.g.  jquery-1.5.js  or  jquery-1.5.min.js Use it from CDN (content delivery network) Microsoft, jQuery, Google CDNs e.g.  http://code.jquery.com/jquery-1.5.min.js , http://ajax.microsoft.com/ajax/jquery/jquery-1.5.min.js
Fundamentals of jQuery Selecting, Adding, Removing DOM Elements
Selecting and Doing Something With jQuery you typically find something, then do something with it Syntax for finding items is the same as the syntax used in CSS to apply styles There are lots of different jQuery methods to do with the selected elements // Finding the item $(&quot;#something&quot;).hide(); // Doing something with the found item <div id=&quot;something&quot;></div>
Show Hide Elements Live Demo
jQuery Fundamentals When selecting with jQuery you can end up with more than one element Any action taken will typically affect all the elements you have selected <div class=&quot;myClass foo bar&quot;></div> <div class=&quot;baz myClass&quot;></div> <div class=&quot;bar&quot;></div> //... $('.myClass').hide(); // will hide both elements //...
DOM Manipulation With jQuery HTML adding elements can be done on the fly Very easily Can be appended to the page Or to another element Still selecting something  (brand new), then doing  something $('<ul><li>Hello</li></ul>').appendTo('body');
// Before <div> <p>Red</p>  <p>Green</p> </div> // Removing elements $('p').remove(); Removing Elements You can also remove elements from the DOM Just as easy // After <div> </div>
Selecting Multiple Elements Live Demo
jQuery Events // Binding an event function() myClickHandler {  // event handling code $(this).css('color', 'red'); }; $('a.tab').click(myClickHandler); With jQuery binding to events is very easy We can specify a click handler  For example by using the click method The above code will bind the  myClickHandler  function to all anchors with a class of  tab
jQuery Events Functions in JavaScript could be anonymous This is the same exact functionality as the previous example This is important because in the previous example we polluted the global scope with a new function name Can be dangerous as someone could overwrite your function with their own accidentally $('a.tab').click(function() {  // event handling code $(this).css('color', 'red'); });
jQuery Method Chaining With jQuery many methods allow chaining Chaining is where you can continue to &quot;chain&quot; on methods one after another As an example, the  addClass   method will add the class ' odd ' in the code below Then return the jQuery collection  We can immediately chain on the &quot; click &quot; event Click  then operates on the odd rows by adding a  click   handler  to each of them $('tr:odd').addClass('odd') .click(function () { alert('you clicked a tr!'); });
Chaining Methods Live Demo
jQuery Stack Architecture Some jQuery methods chain and return a new collection of elements  ' Find ' and ' Filter ' are two examples jQuery holds on to the previous collections, essentially creating a stack set to store them
jQuery Stack Architecture (2) Methods like  Find   and  Filter   create a new collection which is added to the stack Older collections are pushed further ' downward ' on the stack You can get a previous collection back from the stack by using the  end()  method $('body')  // [body] .find('p')  // [p, p, p] > [body] .find('a')  // [a, a] > [p, p, p] > [body] .addClass('foo') .end()  // [p, p, p] > [body] .end()  // [body]
jQuery & Chaining and Architecture This is a popular use that shows both chaining and the stack architecture $('tr') .filter(':odd') .addClass('myOddClass') .end() .filter(':even') .addClass('myEvenClass');
jQuery & Chaining and Architecture (2) First select all rows Then filter to only the odd rows  The odd rows are placed on the top of the stack The ' all rows ' collection is now  'pushed   downward ' Add a class to the odd rows We call end Throws away the ' odd rows ' collection  Grabs the next element in the stack  The ' all rows ' collection Then filter to find  even rows We add a class to the even rows
jQuery Stack Architecture Live Demo
Dynamically Assigning a Class Live Demo
jQuery AJAX
AJAX Fundamentals AJAX is acronym of  Asynchronous JavaScript and XML Technique for background loading of dynamic content and data from the server side Allows dynamic client-side changes Two styles of AJAX Partial page rendering – loading of HTML fragment and showing it in a  <div> JSON service – loading JSON object and client-side processing it with JavaScript / jQuery
jQuery Ajax You can use jQuery Ajax to seamlessly integrate with server side functionality jQuery makes simple the asynchronous server calls jQuery.ajax(…)   The core method for using AJAX functionality The shortcut methods use it 'under the hood' Thus it can do everything $.get(…)  and  $.post(…)   Executes a server-side request and returns a result The HTTP action that will occur  is POST or GET
jQuery Ajax (2) $.getJSON(<url>) Uses the GET HTTP action and inform the server to send back JSON-serialized data $(…).load( <url> ) Gets HTML from the server and loads it into whatever you have selected (e.g. a  <div> ) Note that jQuery AJAX does not use a selection (except for  .load(…)  method) With certain jQuery methods there is not a logical reason to make a selection first  Most AJAX methods fall into that category
jQuery Ajax – $(…).load() Example of dynamically loaded AJAX content: $(…).load(<url>)   Gets an HTML fragment from the server and load it into whatever you have selected Data could come from a PHP script, a static resource or an ASP.NET page Note that the server should return a page fragment If it returns a whole HTML page, then we are going to have some invalid HTML! $('#myContainer').load('home/myHtmlSnippet.html');
jQuery Ajax – Example <button>Perform AJAX Request</button> <script type=&quot;text/javascript&quot;> $(&quot;button&quot;).click(function() { $.ajax({ url: &quot;data.html&quot;, success: function(data){ $('#resultDiv').text(data); } }); }); </script> <div id=&quot;resultDiv&quot;>Result will be shown here</div> Note that  data.html  will not be loaded unless the script comes from a Web server AJAX URL should reside on the same Web server
jQuery AJAX: JSON-Style AJAX and Partial Rendering Live Demo
jQuery UI
jQuery UI jQuery UI is a separate JavaScript library Lives in a separate  .js  file jQuery UI contains three different groups of additions Effects: draggable, droppable, resizable, selectable, sortable Interactions: show & hide additions, color animation, easings Widgets: Accordion, Autocomplete, Button, Datepicker, Dialog, Progressbar, Slider, Tabs
Widgets jQuery widgets are UI components for the Web All widgets are theme-able! Adding most widgets is very simple in code: $(&quot;input:text.date&quot;).datepicker(); $(&quot;#someDiv&quot;).accordion(); var langs = [&quot;C#&quot;, &quot;Java&quot;, &quot;PHP&quot;, &quot;Python&quot;, &quot;SQL&quot;]; $(&quot;#langBox&quot;).autocomplete({ source: langs }); <div id=&quot;dialog&quot; title=&quot;a title&quot;><p>Some text</p></div> $(&quot;#dialog&quot;).dialog(); $(&quot;#slider&quot;).slider();
jQuery UI Live Demo
jQuery UI Drag-and-Drop Live Demo
jQuery Fundamentals Questions? http://schoolacademy.telerik.com
Exercises Open the file  /exercises/index.html  in your browser Select all of the div elements that have a class of &quot; module &quot;. Come up with three selectors that you could use to get the third item in the  #myList  unordered list Select the label for the search input using an attribute selector Count hidden elements on the page  (hint: . length ) Count the image elements that have an alt attribute Select all of the odd table rows in the table body
Exercises (2) Open the file  /exercises/index.html  in your browser Select all of the image elements on the page Log each image's alt attribute Select the search input text box, then traverse up to the form and add a class to the form. Select the list item inside  #myList  that has a class of &quot; current &quot; Remove that class from it Add a class of &quot; current &quot; to the next list item
Exercises (3) Open the file  /exercises/index.html  in your browser Select the select element inside  #specials Traverse your way to the submit button. Select the first list item in the  #slidesh ow element Add the class &quot; current &quot; to it, and then add a class of &quot; disabled &quot; to its sibling elements
Exercises (4) Open the file /exercises/index.html in your browser Add five new list items to the end of the unordered list  #myList Remove the odd list items Add another  h2  and another  paragraph  to the last  div.module Add another option to the select element Give the option the value &quot; Wednesday &quot; Add a new  div.module  to the page after the last one Put a copy of one of the existing images inside of it

More Related Content

What's hot (20)

PDF
Performance Optimization and JavaScript Best Practices
Doris Chen
 
PDF
Javascript Design Patterns
Subramanyan Murali
 
KEY
JavaScript Growing Up
David Padbury
 
PPTX
Javascript best practices
Manav Gupta
 
PDF
Scalable JavaScript Design Patterns
Addy Osmani
 
PPTX
Object Oriented Programming In JavaScript
Forziatech
 
PPT
Object Oriented JavaScript
Donald Sipe
 
PDF
Intro to JavaScript
Jussi Pohjolainen
 
PPT
JavaScript Misunderstood
Bhavya Siddappa
 
PDF
Ten useful JavaScript tips & best practices
Ankit Rastogi
 
PDF
Javascript Best Practices
Christian Heilmann
 
PDF
Modern JavaScript Programming
Wildan Maulana
 
PDF
JavaScript OOPs
Johnson Chou
 
PPTX
Lab #2: Introduction to Javascript
Walid Ashraf
 
PPT
Javascript
mussawir20
 
PDF
JavaScript Objects
Hazem Hagrass
 
PPTX
jQuery PPT
Dominic Arrojado
 
PPTX
Understanding Object Oriented Javascript - Coffee@DBG June
Deepu S Nath
 
PPT
Advanced JavaScript
Stoyan Stefanov
 
Performance Optimization and JavaScript Best Practices
Doris Chen
 
Javascript Design Patterns
Subramanyan Murali
 
JavaScript Growing Up
David Padbury
 
Javascript best practices
Manav Gupta
 
Scalable JavaScript Design Patterns
Addy Osmani
 
Object Oriented Programming In JavaScript
Forziatech
 
Object Oriented JavaScript
Donald Sipe
 
Intro to JavaScript
Jussi Pohjolainen
 
JavaScript Misunderstood
Bhavya Siddappa
 
Ten useful JavaScript tips & best practices
Ankit Rastogi
 
Javascript Best Practices
Christian Heilmann
 
Modern JavaScript Programming
Wildan Maulana
 
JavaScript OOPs
Johnson Chou
 
Lab #2: Introduction to Javascript
Walid Ashraf
 
Javascript
mussawir20
 
JavaScript Objects
Hazem Hagrass
 
jQuery PPT
Dominic Arrojado
 
Understanding Object Oriented Javascript - Coffee@DBG June
Deepu S Nath
 
Advanced JavaScript
Stoyan Stefanov
 

Viewers also liked (13)

PPTX
Verteilte versionsverwaltung mit Team Foundation Server 2012
Daniel Marbach
 
PDF
Ajax for PHP Developers
Michael Girouard
 
PDF
High Performance JavaScript - WebDirections USA 2010
Nicholas Zakas
 
PPT
eXo SEA - JavaScript Introduction Training
Hoat Le
 
PDF
Ajax Introduction Presentation
thinkphp
 
PPT
An Introduction to Ajax Programming
hchen1
 
PDF
HTML5 JavaScript APIs
Remy Sharp
 
PPT
Ajax and PHP
John Coggeshall
 
PDF
Introduction to ajax
Nir Elbaz
 
PDF
How to make Ajax work for you
Simon Willison
 
PPTX
Maintainable JavaScript 2012
Nicholas Zakas
 
PPTX
Introduction to ajax
Raja V
 
Verteilte versionsverwaltung mit Team Foundation Server 2012
Daniel Marbach
 
Ajax for PHP Developers
Michael Girouard
 
High Performance JavaScript - WebDirections USA 2010
Nicholas Zakas
 
eXo SEA - JavaScript Introduction Training
Hoat Le
 
Ajax Introduction Presentation
thinkphp
 
An Introduction to Ajax Programming
hchen1
 
HTML5 JavaScript APIs
Remy Sharp
 
Ajax and PHP
John Coggeshall
 
Introduction to ajax
Nir Elbaz
 
How to make Ajax work for you
Simon Willison
 
Maintainable JavaScript 2012
Nicholas Zakas
 
Introduction to ajax
Raja V
 
Ad

Similar to jQuery Fundamentals (20)

PPTX
jQuery
Jon Erickson
 
PDF
Introduction to jQuery
Zeeshan Khan
 
PDF
Lotusphere 2012 Speedgeeking - jQuery & Domino, a RAD Combination
Sean Burgess
 
PDF
jQuery
Ivano Malavolta
 
PDF
Learning jquery-in-30-minutes-1195942580702664-3
luckysb16
 
PPTX
A to Z about JQuery - Become Newbie to Expert Java Developer
Manoj Bhuva
 
PPTX
Introduction to JQuery
Muhammad Afzal Qureshi
 
PDF
The Dom Scripting Toolkit J Query
QConLondon2008
 
PDF
DOM Scripting Toolkit - jQuery
Remy Sharp
 
PPT
jQuery Tips Tricks Trivia
Cognizant
 
PDF
jQuery (MeshU)
jeresig
 
PDF
Remy Sharp The DOM scripting toolkit jQuery
deimos
 
PDF
jQuery (DrupalCamp Toronto)
jeresig
 
PPT
J query intro_29thsep_alok
SPRITLE SOFTWARE PRIVATE LIMIT ED
 
PPT
Jquery presentation
guest5d87aa6
 
PDF
jQuery in 15 minutes
Simon Willison
 
PPTX
jQuery basics for Beginners
Pooja Saxena
 
PPTX
jQuery
Vishwa Mohan
 
PDF
jQuery Internals + Cool Stuff
jeresig
 
PDF
Introduction to jQuery
Seble Nigussie
 
jQuery
Jon Erickson
 
Introduction to jQuery
Zeeshan Khan
 
Lotusphere 2012 Speedgeeking - jQuery & Domino, a RAD Combination
Sean Burgess
 
Learning jquery-in-30-minutes-1195942580702664-3
luckysb16
 
A to Z about JQuery - Become Newbie to Expert Java Developer
Manoj Bhuva
 
Introduction to JQuery
Muhammad Afzal Qureshi
 
The Dom Scripting Toolkit J Query
QConLondon2008
 
DOM Scripting Toolkit - jQuery
Remy Sharp
 
jQuery Tips Tricks Trivia
Cognizant
 
jQuery (MeshU)
jeresig
 
Remy Sharp The DOM scripting toolkit jQuery
deimos
 
jQuery (DrupalCamp Toronto)
jeresig
 
J query intro_29thsep_alok
SPRITLE SOFTWARE PRIVATE LIMIT ED
 
Jquery presentation
guest5d87aa6
 
jQuery in 15 minutes
Simon Willison
 
jQuery basics for Beginners
Pooja Saxena
 
jQuery
Vishwa Mohan
 
jQuery Internals + Cool Stuff
jeresig
 
Introduction to jQuery
Seble Nigussie
 
Ad

More from Doncho Minkov (20)

PDF
Web Design Concepts
Doncho Minkov
 
PPT
Web design Tools
Doncho Minkov
 
PPT
HTML 5
Doncho Minkov
 
PPT
HTML 5 Tables and Forms
Doncho Minkov
 
PPT
CSS Overview
Doncho Minkov
 
PPT
CSS Presentation
Doncho Minkov
 
PPT
CSS Layout
Doncho Minkov
 
PPT
CSS 3
Doncho Minkov
 
PPT
Adobe Photoshop
Doncho Minkov
 
PPT
Slice and Dice
Doncho Minkov
 
PPT
Introduction to XAML and WPF
Doncho Minkov
 
PPT
WPF Layout Containers
Doncho Minkov
 
PPT
WPF Controls
Doncho Minkov
 
PPT
WPF Templating and Styling
Doncho Minkov
 
PPT
WPF Graphics and Animations
Doncho Minkov
 
PPT
Simple Data Binding
Doncho Minkov
 
PPT
Complex Data Binding
Doncho Minkov
 
PPT
WPF Concepts
Doncho Minkov
 
PPT
Model View ViewModel
Doncho Minkov
 
PPT
WPF and Databases
Doncho Minkov
 
Web Design Concepts
Doncho Minkov
 
Web design Tools
Doncho Minkov
 
HTML 5 Tables and Forms
Doncho Minkov
 
CSS Overview
Doncho Minkov
 
CSS Presentation
Doncho Minkov
 
CSS Layout
Doncho Minkov
 
Adobe Photoshop
Doncho Minkov
 
Slice and Dice
Doncho Minkov
 
Introduction to XAML and WPF
Doncho Minkov
 
WPF Layout Containers
Doncho Minkov
 
WPF Controls
Doncho Minkov
 
WPF Templating and Styling
Doncho Minkov
 
WPF Graphics and Animations
Doncho Minkov
 
Simple Data Binding
Doncho Minkov
 
Complex Data Binding
Doncho Minkov
 
WPF Concepts
Doncho Minkov
 
Model View ViewModel
Doncho Minkov
 
WPF and Databases
Doncho Minkov
 

Recently uploaded (20)

PDF
introduction to computer hardware and sofeware
chauhanshraddha2007
 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
PDF
How ETL Control Logic Keeps Your Pipelines Safe and Reliable.pdf
Stryv Solutions Pvt. Ltd.
 
PDF
Researching The Best Chat SDK Providers in 2025
Ray Fields
 
PPTX
Simple and concise overview about Quantum computing..pptx
mughal641
 
PDF
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
PPTX
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
PDF
Build with AI and GDG Cloud Bydgoszcz- ADK .pdf
jaroslawgajewski1
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PPTX
AI Code Generation Risks (Ramkumar Dilli, CIO, Myridius)
Priyanka Aash
 
PPTX
Farrell_Programming Logic and Design slides_10e_ch02_PowerPoint.pptx
bashnahara11
 
PPTX
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
PDF
State-Dependent Conformal Perception Bounds for Neuro-Symbolic Verification
Ivan Ruchkin
 
PDF
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
PPTX
Agentic AI in Healthcare Driving the Next Wave of Digital Transformation
danielle hunter
 
PPTX
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
PDF
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
PDF
TrustArc Webinar - Navigating Data Privacy in LATAM: Laws, Trends, and Compli...
TrustArc
 
introduction to computer hardware and sofeware
chauhanshraddha2007
 
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
How ETL Control Logic Keeps Your Pipelines Safe and Reliable.pdf
Stryv Solutions Pvt. Ltd.
 
Researching The Best Chat SDK Providers in 2025
Ray Fields
 
Simple and concise overview about Quantum computing..pptx
mughal641
 
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
Build with AI and GDG Cloud Bydgoszcz- ADK .pdf
jaroslawgajewski1
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
AI Code Generation Risks (Ramkumar Dilli, CIO, Myridius)
Priyanka Aash
 
Farrell_Programming Logic and Design slides_10e_ch02_PowerPoint.pptx
bashnahara11
 
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
State-Dependent Conformal Perception Bounds for Neuro-Symbolic Verification
Ivan Ruchkin
 
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
Agentic AI in Healthcare Driving the Next Wave of Digital Transformation
danielle hunter
 
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
TrustArc Webinar - Navigating Data Privacy in LATAM: Laws, Trends, and Compli...
TrustArc
 

jQuery Fundamentals

  • 1. jQuery Fundamentals, DOM, Events, AJAX, UI Doncho Minkov Telerik Mobile Development Course mobiledevcourse.telerik.com Technical Trainer http://www.minkov.it
  • 2. Table of Contents jQuery Fundamentals Selection and DOM Manipulation Events and Chaining AJAX jQuery AJAX Methods Executing AJAX Requests jQuery UI jQuery Widgets Implementing Drag and Drop
  • 3. What is jQuery? The world’s most popular JavaScript library
  • 4. What is jQuery? jQuery is a cross-browser JavaScript library  Designed to simplify the client-side scripting of HTML The most popular JavaScript library in use today Free, open source software jQuery's syntax is designed to make it easier to Navigate a document and select DOM elements Create animations Handle events Develop AJAX applications
  • 5. What is jQuery? (2) jQuery also provides capabilities for developers to create plugins for Low-level interaction and animation Advanced effects and high-level, theme-able widgets Creation of powerful and dynamic web pages Microsoft adopted jQuery within Visual Studio Uses in Microsoft's ASP.NET AJAX Framework and ASP.NET MVC Framework
  • 6. Why jQuery is So Popular? Easy to learn Fluent programming style Easy to extend You create new jQuery plugins by creating new JavaScript functions Powerful DOM Selection Powered by CSS 3.0 Lightweight Community Support Large community of developers and geeks
  • 7. How to Add jQuery to a Web Site? Download jQuery files from http://www.jquery.com Self hosted You can choose to self host the .js file E.g. jquery-1.5.js or jquery-1.5.min.js Use it from CDN (content delivery network) Microsoft, jQuery, Google CDNs e.g. http://code.jquery.com/jquery-1.5.min.js , http://ajax.microsoft.com/ajax/jquery/jquery-1.5.min.js
  • 8. Fundamentals of jQuery Selecting, Adding, Removing DOM Elements
  • 9. Selecting and Doing Something With jQuery you typically find something, then do something with it Syntax for finding items is the same as the syntax used in CSS to apply styles There are lots of different jQuery methods to do with the selected elements // Finding the item $(&quot;#something&quot;).hide(); // Doing something with the found item <div id=&quot;something&quot;></div>
  • 10. Show Hide Elements Live Demo
  • 11. jQuery Fundamentals When selecting with jQuery you can end up with more than one element Any action taken will typically affect all the elements you have selected <div class=&quot;myClass foo bar&quot;></div> <div class=&quot;baz myClass&quot;></div> <div class=&quot;bar&quot;></div> //... $('.myClass').hide(); // will hide both elements //...
  • 12. DOM Manipulation With jQuery HTML adding elements can be done on the fly Very easily Can be appended to the page Or to another element Still selecting something (brand new), then doing something $('<ul><li>Hello</li></ul>').appendTo('body');
  • 13. // Before <div> <p>Red</p> <p>Green</p> </div> // Removing elements $('p').remove(); Removing Elements You can also remove elements from the DOM Just as easy // After <div> </div>
  • 15. jQuery Events // Binding an event function() myClickHandler { // event handling code $(this).css('color', 'red'); }; $('a.tab').click(myClickHandler); With jQuery binding to events is very easy We can specify a click handler For example by using the click method The above code will bind the myClickHandler function to all anchors with a class of tab
  • 16. jQuery Events Functions in JavaScript could be anonymous This is the same exact functionality as the previous example This is important because in the previous example we polluted the global scope with a new function name Can be dangerous as someone could overwrite your function with their own accidentally $('a.tab').click(function() { // event handling code $(this).css('color', 'red'); });
  • 17. jQuery Method Chaining With jQuery many methods allow chaining Chaining is where you can continue to &quot;chain&quot; on methods one after another As an example, the addClass method will add the class ' odd ' in the code below Then return the jQuery collection We can immediately chain on the &quot; click &quot; event Click then operates on the odd rows by adding a click handler to each of them $('tr:odd').addClass('odd') .click(function () { alert('you clicked a tr!'); });
  • 19. jQuery Stack Architecture Some jQuery methods chain and return a new collection of elements ' Find ' and ' Filter ' are two examples jQuery holds on to the previous collections, essentially creating a stack set to store them
  • 20. jQuery Stack Architecture (2) Methods like Find and Filter create a new collection which is added to the stack Older collections are pushed further ' downward ' on the stack You can get a previous collection back from the stack by using the end() method $('body') // [body] .find('p') // [p, p, p] > [body] .find('a') // [a, a] > [p, p, p] > [body] .addClass('foo') .end() // [p, p, p] > [body] .end() // [body]
  • 21. jQuery & Chaining and Architecture This is a popular use that shows both chaining and the stack architecture $('tr') .filter(':odd') .addClass('myOddClass') .end() .filter(':even') .addClass('myEvenClass');
  • 22. jQuery & Chaining and Architecture (2) First select all rows Then filter to only the odd rows The odd rows are placed on the top of the stack The ' all rows ' collection is now 'pushed downward ' Add a class to the odd rows We call end Throws away the ' odd rows ' collection Grabs the next element in the stack The ' all rows ' collection Then filter to find even rows We add a class to the even rows
  • 24. Dynamically Assigning a Class Live Demo
  • 26. AJAX Fundamentals AJAX is acronym of Asynchronous JavaScript and XML Technique for background loading of dynamic content and data from the server side Allows dynamic client-side changes Two styles of AJAX Partial page rendering – loading of HTML fragment and showing it in a <div> JSON service – loading JSON object and client-side processing it with JavaScript / jQuery
  • 27. jQuery Ajax You can use jQuery Ajax to seamlessly integrate with server side functionality jQuery makes simple the asynchronous server calls jQuery.ajax(…) The core method for using AJAX functionality The shortcut methods use it 'under the hood' Thus it can do everything $.get(…) and $.post(…) Executes a server-side request and returns a result The HTTP action that will occur is POST or GET
  • 28. jQuery Ajax (2) $.getJSON(<url>) Uses the GET HTTP action and inform the server to send back JSON-serialized data $(…).load( <url> ) Gets HTML from the server and loads it into whatever you have selected (e.g. a <div> ) Note that jQuery AJAX does not use a selection (except for .load(…) method) With certain jQuery methods there is not a logical reason to make a selection first Most AJAX methods fall into that category
  • 29. jQuery Ajax – $(…).load() Example of dynamically loaded AJAX content: $(…).load(<url>) Gets an HTML fragment from the server and load it into whatever you have selected Data could come from a PHP script, a static resource or an ASP.NET page Note that the server should return a page fragment If it returns a whole HTML page, then we are going to have some invalid HTML! $('#myContainer').load('home/myHtmlSnippet.html');
  • 30. jQuery Ajax – Example <button>Perform AJAX Request</button> <script type=&quot;text/javascript&quot;> $(&quot;button&quot;).click(function() { $.ajax({ url: &quot;data.html&quot;, success: function(data){ $('#resultDiv').text(data); } }); }); </script> <div id=&quot;resultDiv&quot;>Result will be shown here</div> Note that data.html will not be loaded unless the script comes from a Web server AJAX URL should reside on the same Web server
  • 31. jQuery AJAX: JSON-Style AJAX and Partial Rendering Live Demo
  • 33. jQuery UI jQuery UI is a separate JavaScript library Lives in a separate .js file jQuery UI contains three different groups of additions Effects: draggable, droppable, resizable, selectable, sortable Interactions: show & hide additions, color animation, easings Widgets: Accordion, Autocomplete, Button, Datepicker, Dialog, Progressbar, Slider, Tabs
  • 34. Widgets jQuery widgets are UI components for the Web All widgets are theme-able! Adding most widgets is very simple in code: $(&quot;input:text.date&quot;).datepicker(); $(&quot;#someDiv&quot;).accordion(); var langs = [&quot;C#&quot;, &quot;Java&quot;, &quot;PHP&quot;, &quot;Python&quot;, &quot;SQL&quot;]; $(&quot;#langBox&quot;).autocomplete({ source: langs }); <div id=&quot;dialog&quot; title=&quot;a title&quot;><p>Some text</p></div> $(&quot;#dialog&quot;).dialog(); $(&quot;#slider&quot;).slider();
  • 37. jQuery Fundamentals Questions? http://schoolacademy.telerik.com
  • 38. Exercises Open the file /exercises/index.html in your browser Select all of the div elements that have a class of &quot; module &quot;. Come up with three selectors that you could use to get the third item in the #myList unordered list Select the label for the search input using an attribute selector Count hidden elements on the page (hint: . length ) Count the image elements that have an alt attribute Select all of the odd table rows in the table body
  • 39. Exercises (2) Open the file /exercises/index.html in your browser Select all of the image elements on the page Log each image's alt attribute Select the search input text box, then traverse up to the form and add a class to the form. Select the list item inside #myList that has a class of &quot; current &quot; Remove that class from it Add a class of &quot; current &quot; to the next list item
  • 40. Exercises (3) Open the file /exercises/index.html in your browser Select the select element inside #specials Traverse your way to the submit button. Select the first list item in the #slidesh ow element Add the class &quot; current &quot; to it, and then add a class of &quot; disabled &quot; to its sibling elements
  • 41. Exercises (4) Open the file /exercises/index.html in your browser Add five new list items to the end of the unordered list #myList Remove the odd list items Add another h2 and another paragraph to the last div.module Add another option to the select element Give the option the value &quot; Wednesday &quot; Add a new div.module to the page after the last one Put a copy of one of the existing images inside of it

Editor's Notes

  • #8: Talking Points: You can choose to self host files - this is including jQuery in the scripts folder in Visual Studio. Can also just include jQuery from a CDN - simply change the script reference to point to jQuery on the CDN Can be faster loading and client browser may already have jQuery file cached Be careful! If the CDN goes down your site may also go down. Source version is human readable. Always include the minified version for your production code.