SlideShare uma empresa Scribd logo
1 de 17
Baixar para ler offline
JavaScript the Smart Way
 Getting Started with jQuery
 Drupal User Group presentation
Introduction to jQuery
• JavaScript / jQuery / AJAX - what’s the
  difference?
  – JavaScript is a scripting language that adds
    interactivity to web pages
  – jQuery is a JavaScript Framework
  – AJAX is a particular type of functionality
• JavaScript is to jQuery as PHP is to
  Drupal
Introduction to jQuery
• What can jQuery do for me?




   “FLASH”Y STUFF



                    AJAX


                               TABS
Introduction to jQuery
• Advantages of jQuery over plain JavaScript
  – Simplifies cross-browser issues
  – You don’t have to write the same code over and
    over again
• Advantages of jQuery over other JS
  frameworks
  – Very concise code
  – Small file size
  – completely open source
  – Plugin architecture
jQuery Basics
• What do I need to get started?
  – An understanding of CSS and the DOM
  – Basic understanding of JS syntax (ideally!)
  – jQuery itself
    http://docs.jquery.com/Downloading_jQuery
• $(document).ready(function(){
      //let’s get started!
  });
Selectors
• CSS
 $(‘a’), $(‘#container’),
 $(‘div.ajaxContainer’),
 $(‘li:first-child’)
• X-Path:
 $(‘a[title]’), $(‘div[ul]’),
 $(‘a[href^=“mailto:”]’)
• Custom:
 $(‘li:eq(1)’),
 $(‘tr:not([th]):odd’)
Some Useful Methods

• DOM Traversal
  – .parent(), .siblings(), .next()
• Manipulation
  – .html(), .empty(), .append(content)
• Events
  – .ready(fn), .hover(fn1, fn2), .click(fn)
• Effects
  – .slideToggle(), .show(), .hide()
Chaining

• $(‘#someElement’)
  .parent().parent()
  .find(‘div.green’)
  .hide().end()
  .siblings().find(‘div.blue’)
  .show().end()
  .parent().next()
  .addClass(‘redBorder’);
Show/Hide Example
$(document).ready(function() {
  $('a.showhide').click(function()
  {
     $(this).parent().parent()
       .find('div.view-data-body')
       .slideToggle();
     return false;
    });
});                         see example
jQuery in Drupal
• drupal_add_js($data, $type)
  – Add a JavaScript file, setting or inline code to the
    page, for example:
      • drupal_add_js(drupal_get_path(‘module’, ‘mymodule’)
        .'/myjs.js');
      • drupal_add_js(array(‘myjs’=>$mysettings), ‘setting’);
      • Drupal_add_js(‘var myVar = “foo”;’, ‘inline’);
• Where do I put my code?
  – themers: put your .js file in your theme directory and call
    drupal_add_js(drupal_get_path(‘theme’, ‘mytheme’) .
    ‘myjs.js’) from a tpl file
  – module developers: put your .js file in your module directory
    and call drupal_add_js() before you output content
Ajaxifying Drupal with jQuery
• Basic essentials:
  – jQuery’s .ajax() or .get() method
  – drupal/path
  – callback function
• drupal_to_js($var)
  – Converts a PHP variable into its JavaScript
    equivalent.
• Drupal.parseJSON(data)
Ajaxifying Drupal with jQuery




                         12
Ajaxifying Drupal with jQuery
‣ $items[] = array('path' => 'ajax/path',
    'type' => MENU_CALLBACK,
    'access' => TRUE,
    'callback' => 'get_ajax_image');
‣ function get_ajax_image($nid) {
     //some code to retrieve the image
     print drupal_to_js(array(
     ‘title’ => $image_title,
     ‘path’ => $image_path
    ));
  }
Ajaxifying Drupal with jQuery

‣ $(‘a.ajax-button’).click(function() {
    $.get(this.href, function(data){
      var result = Drupal.parseJSON(data);
    $('div.title').html(result['title']);
      $('div.image').html(result['path']);
    });
    return false;
  });
Resources

• http://jquery.com
• Book: Learning jQuery
          » PACKT Publishing

• Cheat sheets:
          » http://colorcharge.com/jquery

• Other online resources
          » http://www.learningjquery.com
          » http://15daysofjquery.com
          » http://visualjquery.com
Quick Tabs


       Create blocks of tabbed views!




http://drupal.org/project/quicktabs
Key Takeaways
• jQuery is a framework for writing JavaScript
• It is extremely concise and therefore easy to
  learn
• It has a robust and efficient CSS-based
  selector mechanism for precise selection of
  DOM elements
• It is modular, so non-standard features are
  available as plugins
• AJAX is a piece of cake with jQuery
• Drupal ships with jQuery already built in

Mais conteúdo relacionado

Mais procurados

Short intro to JQuery and Modernizr
Short intro to JQuery and ModernizrShort intro to JQuery and Modernizr
Short intro to JQuery and Modernizr
Jussi Pohjolainen
 
код презентации
код презентациикод презентации
код презентации
LFadina62
 
CT presentatie JQuery 7.12.11
CT presentatie JQuery 7.12.11CT presentatie JQuery 7.12.11
CT presentatie JQuery 7.12.11
virtualsciences41
 
How to learn j query
How to learn j queryHow to learn j query
How to learn j query
Baoyu Xu
 

Mais procurados (19)

An in-depth look at jQuery UI
An in-depth look at jQuery UIAn in-depth look at jQuery UI
An in-depth look at jQuery UI
 
An in-depth look at jQuery
An in-depth look at jQueryAn in-depth look at jQuery
An in-depth look at jQuery
 
php Slideshare
php Slidesharephp Slideshare
php Slideshare
 
Short intro to JQuery and Modernizr
Short intro to JQuery and ModernizrShort intro to JQuery and Modernizr
Short intro to JQuery and Modernizr
 
bcgr3-jquery
bcgr3-jquerybcgr3-jquery
bcgr3-jquery
 
Jquery for post a form
Jquery for post a formJquery for post a form
Jquery for post a form
 
JQuery
JQueryJQuery
JQuery
 
State of jQuery and Drupal
State of jQuery and DrupalState of jQuery and Drupal
State of jQuery and Drupal
 
код презентации
код презентациикод презентации
код презентации
 
Jquery presentation
Jquery presentationJquery presentation
Jquery presentation
 
CT presentatie JQuery 7.12.11
CT presentatie JQuery 7.12.11CT presentatie JQuery 7.12.11
CT presentatie JQuery 7.12.11
 
Best Practices for Embedded UA - WritersUA 2012, Scott DeLoach, ClickStart
Best Practices for Embedded UA - WritersUA 2012, Scott DeLoach, ClickStartBest Practices for Embedded UA - WritersUA 2012, Scott DeLoach, ClickStart
Best Practices for Embedded UA - WritersUA 2012, Scott DeLoach, ClickStart
 
Introduction to jQuery (Ajax Exp 2007)
Introduction to jQuery (Ajax Exp 2007)Introduction to jQuery (Ajax Exp 2007)
Introduction to jQuery (Ajax Exp 2007)
 
ActiveDOM
ActiveDOMActiveDOM
ActiveDOM
 
Drupal Javascript for developers
Drupal Javascript for developersDrupal Javascript for developers
Drupal Javascript for developers
 
How to learn j query
How to learn j queryHow to learn j query
How to learn j query
 
HTML5: A primer on the web's present and future
HTML5: A primer on the web's present and futureHTML5: A primer on the web's present and future
HTML5: A primer on the web's present and future
 
Video gouache
Video gouacheVideo gouache
Video gouache
 
Advanced jQuery (Ajax Exp 2007)
Advanced jQuery (Ajax Exp 2007)Advanced jQuery (Ajax Exp 2007)
Advanced jQuery (Ajax Exp 2007)
 

Destaque (7)

jQuery Beginner
jQuery BeginnerjQuery Beginner
jQuery Beginner
 
webstudy jquery
webstudy jquerywebstudy jquery
webstudy jquery
 
jQuery Best Practice
jQuery Best Practice jQuery Best Practice
jQuery Best Practice
 
Introductory css and j query
Introductory css and j queryIntroductory css and j query
Introductory css and j query
 
Android Project Presentation
Android Project PresentationAndroid Project Presentation
Android Project Presentation
 
JQuery: JavaScript Library of the Future
JQuery: JavaScript Library of the FutureJQuery: JavaScript Library of the Future
JQuery: JavaScript Library of the Future
 
J query 17-visual-cheat-sheet
J query 17-visual-cheat-sheetJ query 17-visual-cheat-sheet
J query 17-visual-cheat-sheet
 

Semelhante a JavaScript the Smart Way - Getting Started with jQuery

Dependency Management with RequireJS
Dependency Management with RequireJSDependency Management with RequireJS
Dependency Management with RequireJS
Aaronius
 
Remy Sharp The DOM scripting toolkit jQuery
Remy Sharp The DOM scripting toolkit jQueryRemy Sharp The DOM scripting toolkit jQuery
Remy Sharp The DOM scripting toolkit jQuery
deimos
 

Semelhante a JavaScript the Smart Way - Getting Started with jQuery (20)

jQuery Presentation to Rails Developers
jQuery Presentation to Rails DevelopersjQuery Presentation to Rails Developers
jQuery Presentation to Rails Developers
 
Drupal & javascript
Drupal & javascriptDrupal & javascript
Drupal & javascript
 
jQuery (DrupalCamp Toronto)
jQuery (DrupalCamp Toronto)jQuery (DrupalCamp Toronto)
jQuery (DrupalCamp Toronto)
 
How to make Ajax Libraries work for you
How to make Ajax Libraries work for youHow to make Ajax Libraries work for you
How to make Ajax Libraries work for you
 
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...
 
DOM Scripting Toolkit - jQuery
DOM Scripting Toolkit - jQueryDOM Scripting Toolkit - jQuery
DOM Scripting Toolkit - jQuery
 
Dependency Management with RequireJS
Dependency Management with RequireJSDependency Management with RequireJS
Dependency Management with RequireJS
 
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
 
Styling components with JavaScript
Styling components with JavaScriptStyling components with JavaScript
Styling components with JavaScript
 
[Coscup 2012] JavascriptMVC
[Coscup 2012] JavascriptMVC[Coscup 2012] JavascriptMVC
[Coscup 2012] JavascriptMVC
 
Everything is Awesome - Cutting the Corners off the Web
Everything is Awesome - Cutting the Corners off the WebEverything is Awesome - Cutting the Corners off the Web
Everything is Awesome - Cutting the Corners off the Web
 
Drupal.Behaviors
Drupal.BehaviorsDrupal.Behaviors
Drupal.Behaviors
 
Remy Sharp The DOM scripting toolkit jQuery
Remy Sharp The DOM scripting toolkit jQueryRemy Sharp The DOM scripting toolkit jQuery
Remy Sharp The DOM scripting toolkit jQuery
 
Javascript first-class citizenery
Javascript first-class citizeneryJavascript first-class citizenery
Javascript first-class citizenery
 
jQuery & 10,000 Global Functions: Working with Legacy JavaScript
jQuery & 10,000 Global Functions: Working with Legacy JavaScriptjQuery & 10,000 Global Functions: Working with Legacy JavaScript
jQuery & 10,000 Global Functions: Working with Legacy JavaScript
 
bcgr3-jquery
bcgr3-jquerybcgr3-jquery
bcgr3-jquery
 
Jazz up your JavaScript: Unobtrusive scripting with JavaScript libraries
Jazz up your JavaScript: Unobtrusive scripting with JavaScript librariesJazz up your JavaScript: Unobtrusive scripting with JavaScript libraries
Jazz up your JavaScript: Unobtrusive scripting with JavaScript libraries
 
Integrating React.js Into a PHP Application
Integrating React.js Into a PHP ApplicationIntegrating React.js Into a PHP Application
Integrating React.js Into a PHP Application
 
Jquery News Packages
Jquery News PackagesJquery News Packages
Jquery News Packages
 
Streamlining Your Applications with Web Frameworks
Streamlining Your Applications with Web FrameworksStreamlining Your Applications with Web Frameworks
Streamlining Your Applications with Web Frameworks
 

Último

FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
dollysharma2066
 
Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
amitlee9823
 
Call Girls in Delhi, Escort Service Available 24x7 in Delhi 959961-/-3876
Call Girls in Delhi, Escort Service Available 24x7 in Delhi 959961-/-3876Call Girls in Delhi, Escort Service Available 24x7 in Delhi 959961-/-3876
Call Girls in Delhi, Escort Service Available 24x7 in Delhi 959961-/-3876
dlhescort
 
Quick Doctor In Kuwait +2773`7758`557 Kuwait Doha Qatar Dubai Abu Dhabi Sharj...
Quick Doctor In Kuwait +2773`7758`557 Kuwait Doha Qatar Dubai Abu Dhabi Sharj...Quick Doctor In Kuwait +2773`7758`557 Kuwait Doha Qatar Dubai Abu Dhabi Sharj...
Quick Doctor In Kuwait +2773`7758`557 Kuwait Doha Qatar Dubai Abu Dhabi Sharj...
daisycvs
 
Call Girls From Raj Nagar Extension Ghaziabad❤️8448577510 ⊹Best Escorts Servi...
Call Girls From Raj Nagar Extension Ghaziabad❤️8448577510 ⊹Best Escorts Servi...Call Girls From Raj Nagar Extension Ghaziabad❤️8448577510 ⊹Best Escorts Servi...
Call Girls From Raj Nagar Extension Ghaziabad❤️8448577510 ⊹Best Escorts Servi...
lizamodels9
 
Call Now ☎️🔝 9332606886🔝 Call Girls ❤ Service In Bhilwara Female Escorts Serv...
Call Now ☎️🔝 9332606886🔝 Call Girls ❤ Service In Bhilwara Female Escorts Serv...Call Now ☎️🔝 9332606886🔝 Call Girls ❤ Service In Bhilwara Female Escorts Serv...
Call Now ☎️🔝 9332606886🔝 Call Girls ❤ Service In Bhilwara Female Escorts Serv...
Anamikakaur10
 
Russian Call Girls In Rajiv Chowk Gurgaon ❤️8448577510 ⊹Best Escorts Service ...
Russian Call Girls In Rajiv Chowk Gurgaon ❤️8448577510 ⊹Best Escorts Service ...Russian Call Girls In Rajiv Chowk Gurgaon ❤️8448577510 ⊹Best Escorts Service ...
Russian Call Girls In Rajiv Chowk Gurgaon ❤️8448577510 ⊹Best Escorts Service ...
lizamodels9
 
Call Girls Kengeri Satellite Town Just Call 👗 7737669865 👗 Top Class Call Gir...
Call Girls Kengeri Satellite Town Just Call 👗 7737669865 👗 Top Class Call Gir...Call Girls Kengeri Satellite Town Just Call 👗 7737669865 👗 Top Class Call Gir...
Call Girls Kengeri Satellite Town Just Call 👗 7737669865 👗 Top Class Call Gir...
amitlee9823
 

Último (20)

Cracking the Cultural Competence Code.pptx
Cracking the Cultural Competence Code.pptxCracking the Cultural Competence Code.pptx
Cracking the Cultural Competence Code.pptx
 
👉Chandigarh Call Girls 👉9878799926👉Just Call👉Chandigarh Call Girl In Chandiga...
👉Chandigarh Call Girls 👉9878799926👉Just Call👉Chandigarh Call Girl In Chandiga...👉Chandigarh Call Girls 👉9878799926👉Just Call👉Chandigarh Call Girl In Chandiga...
👉Chandigarh Call Girls 👉9878799926👉Just Call👉Chandigarh Call Girl In Chandiga...
 
Malegaon Call Girls Service ☎ ️82500–77686 ☎️ Enjoy 24/7 Escort Service
Malegaon Call Girls Service ☎ ️82500–77686 ☎️ Enjoy 24/7 Escort ServiceMalegaon Call Girls Service ☎ ️82500–77686 ☎️ Enjoy 24/7 Escort Service
Malegaon Call Girls Service ☎ ️82500–77686 ☎️ Enjoy 24/7 Escort Service
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
Falcon Invoice Discounting platform in india
Falcon Invoice Discounting platform in indiaFalcon Invoice Discounting platform in india
Falcon Invoice Discounting platform in india
 
Marel Q1 2024 Investor Presentation from May 8, 2024
Marel Q1 2024 Investor Presentation from May 8, 2024Marel Q1 2024 Investor Presentation from May 8, 2024
Marel Q1 2024 Investor Presentation from May 8, 2024
 
Call Girls Service In Old Town Dubai ((0551707352)) Old Town Dubai Call Girl ...
Call Girls Service In Old Town Dubai ((0551707352)) Old Town Dubai Call Girl ...Call Girls Service In Old Town Dubai ((0551707352)) Old Town Dubai Call Girl ...
Call Girls Service In Old Town Dubai ((0551707352)) Old Town Dubai Call Girl ...
 
Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
 
Call Girls in Delhi, Escort Service Available 24x7 in Delhi 959961-/-3876
Call Girls in Delhi, Escort Service Available 24x7 in Delhi 959961-/-3876Call Girls in Delhi, Escort Service Available 24x7 in Delhi 959961-/-3876
Call Girls in Delhi, Escort Service Available 24x7 in Delhi 959961-/-3876
 
Quick Doctor In Kuwait +2773`7758`557 Kuwait Doha Qatar Dubai Abu Dhabi Sharj...
Quick Doctor In Kuwait +2773`7758`557 Kuwait Doha Qatar Dubai Abu Dhabi Sharj...Quick Doctor In Kuwait +2773`7758`557 Kuwait Doha Qatar Dubai Abu Dhabi Sharj...
Quick Doctor In Kuwait +2773`7758`557 Kuwait Doha Qatar Dubai Abu Dhabi Sharj...
 
Business Model Canvas (BMC)- A new venture concept
Business Model Canvas (BMC)-  A new venture conceptBusiness Model Canvas (BMC)-  A new venture concept
Business Model Canvas (BMC)- A new venture concept
 
Call Girls From Raj Nagar Extension Ghaziabad❤️8448577510 ⊹Best Escorts Servi...
Call Girls From Raj Nagar Extension Ghaziabad❤️8448577510 ⊹Best Escorts Servi...Call Girls From Raj Nagar Extension Ghaziabad❤️8448577510 ⊹Best Escorts Servi...
Call Girls From Raj Nagar Extension Ghaziabad❤️8448577510 ⊹Best Escorts Servi...
 
Call Now ☎️🔝 9332606886🔝 Call Girls ❤ Service In Bhilwara Female Escorts Serv...
Call Now ☎️🔝 9332606886🔝 Call Girls ❤ Service In Bhilwara Female Escorts Serv...Call Now ☎️🔝 9332606886🔝 Call Girls ❤ Service In Bhilwara Female Escorts Serv...
Call Now ☎️🔝 9332606886🔝 Call Girls ❤ Service In Bhilwara Female Escorts Serv...
 
The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...
The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...
The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...
 
Lundin Gold - Q1 2024 Conference Call Presentation (Revised)
Lundin Gold - Q1 2024 Conference Call Presentation (Revised)Lundin Gold - Q1 2024 Conference Call Presentation (Revised)
Lundin Gold - Q1 2024 Conference Call Presentation (Revised)
 
Russian Call Girls In Rajiv Chowk Gurgaon ❤️8448577510 ⊹Best Escorts Service ...
Russian Call Girls In Rajiv Chowk Gurgaon ❤️8448577510 ⊹Best Escorts Service ...Russian Call Girls In Rajiv Chowk Gurgaon ❤️8448577510 ⊹Best Escorts Service ...
Russian Call Girls In Rajiv Chowk Gurgaon ❤️8448577510 ⊹Best Escorts Service ...
 
(Anamika) VIP Call Girls Napur Call Now 8617697112 Napur Escorts 24x7
(Anamika) VIP Call Girls Napur Call Now 8617697112 Napur Escorts 24x7(Anamika) VIP Call Girls Napur Call Now 8617697112 Napur Escorts 24x7
(Anamika) VIP Call Girls Napur Call Now 8617697112 Napur Escorts 24x7
 
Dr. Admir Softic_ presentation_Green Club_ENG.pdf
Dr. Admir Softic_ presentation_Green Club_ENG.pdfDr. Admir Softic_ presentation_Green Club_ENG.pdf
Dr. Admir Softic_ presentation_Green Club_ENG.pdf
 
SEO Case Study: How I Increased SEO Traffic & Ranking by 50-60% in 6 Months
SEO Case Study: How I Increased SEO Traffic & Ranking by 50-60%  in 6 MonthsSEO Case Study: How I Increased SEO Traffic & Ranking by 50-60%  in 6 Months
SEO Case Study: How I Increased SEO Traffic & Ranking by 50-60% in 6 Months
 
Call Girls Kengeri Satellite Town Just Call 👗 7737669865 👗 Top Class Call Gir...
Call Girls Kengeri Satellite Town Just Call 👗 7737669865 👗 Top Class Call Gir...Call Girls Kengeri Satellite Town Just Call 👗 7737669865 👗 Top Class Call Gir...
Call Girls Kengeri Satellite Town Just Call 👗 7737669865 👗 Top Class Call Gir...
 

JavaScript the Smart Way - Getting Started with jQuery

  • 1. JavaScript the Smart Way Getting Started with jQuery Drupal User Group presentation
  • 2. Introduction to jQuery • JavaScript / jQuery / AJAX - what’s the difference? – JavaScript is a scripting language that adds interactivity to web pages – jQuery is a JavaScript Framework – AJAX is a particular type of functionality • JavaScript is to jQuery as PHP is to Drupal
  • 3. Introduction to jQuery • What can jQuery do for me? “FLASH”Y STUFF AJAX TABS
  • 4. Introduction to jQuery • Advantages of jQuery over plain JavaScript – Simplifies cross-browser issues – You don’t have to write the same code over and over again • Advantages of jQuery over other JS frameworks – Very concise code – Small file size – completely open source – Plugin architecture
  • 5. jQuery Basics • What do I need to get started? – An understanding of CSS and the DOM – Basic understanding of JS syntax (ideally!) – jQuery itself http://docs.jquery.com/Downloading_jQuery • $(document).ready(function(){ //let’s get started! });
  • 6. Selectors • CSS $(‘a’), $(‘#container’), $(‘div.ajaxContainer’), $(‘li:first-child’) • X-Path: $(‘a[title]’), $(‘div[ul]’), $(‘a[href^=“mailto:”]’) • Custom: $(‘li:eq(1)’), $(‘tr:not([th]):odd’)
  • 7. Some Useful Methods • DOM Traversal – .parent(), .siblings(), .next() • Manipulation – .html(), .empty(), .append(content) • Events – .ready(fn), .hover(fn1, fn2), .click(fn) • Effects – .slideToggle(), .show(), .hide()
  • 8. Chaining • $(‘#someElement’) .parent().parent() .find(‘div.green’) .hide().end() .siblings().find(‘div.blue’) .show().end() .parent().next() .addClass(‘redBorder’);
  • 9. Show/Hide Example $(document).ready(function() { $('a.showhide').click(function() { $(this).parent().parent() .find('div.view-data-body') .slideToggle(); return false; }); }); see example
  • 10. jQuery in Drupal • drupal_add_js($data, $type) – Add a JavaScript file, setting or inline code to the page, for example: • drupal_add_js(drupal_get_path(‘module’, ‘mymodule’) .'/myjs.js'); • drupal_add_js(array(‘myjs’=>$mysettings), ‘setting’); • Drupal_add_js(‘var myVar = “foo”;’, ‘inline’); • Where do I put my code? – themers: put your .js file in your theme directory and call drupal_add_js(drupal_get_path(‘theme’, ‘mytheme’) . ‘myjs.js’) from a tpl file – module developers: put your .js file in your module directory and call drupal_add_js() before you output content
  • 11. Ajaxifying Drupal with jQuery • Basic essentials: – jQuery’s .ajax() or .get() method – drupal/path – callback function • drupal_to_js($var) – Converts a PHP variable into its JavaScript equivalent. • Drupal.parseJSON(data)
  • 13. Ajaxifying Drupal with jQuery ‣ $items[] = array('path' => 'ajax/path', 'type' => MENU_CALLBACK, 'access' => TRUE, 'callback' => 'get_ajax_image'); ‣ function get_ajax_image($nid) { //some code to retrieve the image print drupal_to_js(array( ‘title’ => $image_title, ‘path’ => $image_path )); }
  • 14. Ajaxifying Drupal with jQuery ‣ $(‘a.ajax-button’).click(function() { $.get(this.href, function(data){ var result = Drupal.parseJSON(data); $('div.title').html(result['title']); $('div.image').html(result['path']); }); return false; });
  • 15. Resources • http://jquery.com • Book: Learning jQuery » PACKT Publishing • Cheat sheets: » http://colorcharge.com/jquery • Other online resources » http://www.learningjquery.com » http://15daysofjquery.com » http://visualjquery.com
  • 16. Quick Tabs Create blocks of tabbed views! http://drupal.org/project/quicktabs
  • 17. Key Takeaways • jQuery is a framework for writing JavaScript • It is extremely concise and therefore easy to learn • It has a robust and efficient CSS-based selector mechanism for precise selection of DOM elements • It is modular, so non-standard features are available as plugins • AJAX is a piece of cake with jQuery • Drupal ships with jQuery already built in