SlideShare a Scribd company logo
10 Time-Savers You (Maybe) Don't Know




           Girish Gangadharan


@appoosa        girish@giri.sh      http://giri.sh
Limit DOM traversal

var $items = $(„#products‟);

$items.click(function() {
          $(this).next(„div‟).slideToggle();
});

$items.addClass(„active‟);

$items.closest(„p#category‟).animate({ height:100px });
Less code. Better readability.

var $userprofile = $(„#user-profile‟);
$userprofile
     .hover(function () {
                 $(this).addClass(“highlight");
            },
            function () {
                 $(this).removeClass(“highlight");
      })
     .click(function() {
          //do something
     })
     .fadeIn(„slow‟);
Don‟t leave them hanging.


    :first-child, :last-child, :even, :odd, :parent, :next, :prev, etc.



Precede them with a tag name or some selector;
      else the universal selector is implied.

                     $(':focus') = $('*:focus')
Understand what each does. Use appropriately.

         $('#sometable').each(function(){
                $('td', this).bind('hover', function(){
                      $(this).toggleClass('hover');
                });
          });
         $('#sometable').each(function(){
                $('td', this).live('hover', function(){
                      $(this).toggleClass('hover');
                });
          });
         $('#sometable').delegate('td', 'hover', function(){
                $(this).toggleClass('hover');
         });
Create in memory. Then update the DOM.

var $mylist = $('#mylist'); // <ul>
for (var i = 0; i < 100; i++) {
           $mylist.append('<li>' + bestsellers[i] + '</li>');
}



var $mylist = $('#mylist'); // <ul>
var concatenatedList = “”;
for (var i = 0; i < 100; i++) {
          concatenatedList += ('<li>' + bestsellers[i] + '</li>');
}
$mylist.append(concatenatedList);
Bind less.


$(„#reports td‟).bind(„click‟, function() {
      //do something
})




$(„#reports‟).bind(„click‟, function(e) {
     var target = e.target;
     if (target.nodeName === „td') {
                // do something
     }
})
Choose your event carefully.


$(document).ready(function() {
     //Fires as soon as the DOM is ready
     //Doesn‟t wait for images, style sheets etc.
})


$(window).load(function() {
     //Fires after all the content is loaded
     //Includes images, style sheets, etc.
})
Think right-to-left (except for IDs)



 $('#user-profiles > li > input:disabled');




$('#user-profiles‟).find(„li > input:disabled');
Sizzle engine is highly optimized but…

var $someElement = $(„#elementId‟);
                 Vs.
var someElement = document.getElementById(„elementId‟);



                 $(„#elementId‟).css(„display‟, „block‟);
                                      Vs.
                 document.getElementById(„elementId‟).style.display = „block‟;


             Do you really need a whole library to
                   accomplish your tasks?
Will this improve page performance by x times?

                        Probably, not.
(especially if you don‟t have a complex multi-nested DOM)




       For example, how to better structure your code.
           • Module Pattern
           • Revealing Module Pattern
           • Constructional Pattern
           • Creational Pattern
           • Factory Pattern
           • etc.
jQuery - 10 Time-Savers You (Maybe) Don't Know
jQuery - 10 Time-Savers You (Maybe) Don't Know
Essential JavaScript Design Patterns: (Free!)
http://addyosmani.com/blog/essentialjsdesignpatternsupdate1


jQuery: Novice to Ninja
http://www.amazon.com/jQuery-Novice-Ninja-Earle-Castledine/dp/0980576857



                            jQuery API
                            http://api.jquery.com



                            JavaScript Performance Testing
                            http://jsperf.com/browse
10 Time-Savers You (Maybe) Don't Know




           Girish Gangadharan


@appoosa        girish@giri.sh      http://giri.sh

More Related Content

PDF
Prototype & jQuery
ODP
Introduction to jQuery
PPTX
Auto tools
PDF
Cheap frontend tricks
PDF
jQuery in 15 minutes
PDF
Railsbridge javascript
PDF
Let jQuery Rock Your World
PDF
Drupal, meet Assetic
Prototype & jQuery
Introduction to jQuery
Auto tools
Cheap frontend tricks
jQuery in 15 minutes
Railsbridge javascript
Let jQuery Rock Your World
Drupal, meet Assetic

What's hot (20)

PDF
jQuery Loves Developers - Oredev 2009
PDF
jQuery Essentials
KEY
Introduction to jQuery - Barcamp London 9
ODP
Drupal Best Practices
PPTX
jQuery Best Practice
PDF
jQuery Essentials
PPT
How to learn j query
PDF
Write Less Do More
PPTX
jQuery Foot-Gun Features
PDF
jQuery for beginners
PDF
Mojolicious: what works and what doesn't
PPTX
Getting the Most Out of jQuery Widgets
PDF
Intro to jquery
PDF
J queryui
PDF
Mojolicious, real-time web framework
PPTX
Jquery introduction
ODP
Mojolicious on Steroids
PPTX
Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)
PPTX
Jquery-overview
jQuery Loves Developers - Oredev 2009
jQuery Essentials
Introduction to jQuery - Barcamp London 9
Drupal Best Practices
jQuery Best Practice
jQuery Essentials
How to learn j query
Write Less Do More
jQuery Foot-Gun Features
jQuery for beginners
Mojolicious: what works and what doesn't
Getting the Most Out of jQuery Widgets
Intro to jquery
J queryui
Mojolicious, real-time web framework
Jquery introduction
Mojolicious on Steroids
Mojolicious - Perl Framework for the Real-Time Web (Lightning Talk)
Jquery-overview
Ad

Viewers also liked (13)

PPT
Presentación Gonzalo Cubillos
PPS
Ghanshyam Asked To Krishna
PPT
Building your-personal-brand-ver-0-7
PPTX
#Ti2016 Tijuana Innovadora 2016, Creativa
PDF
Le collectif du 4 Décembre écrit au pouvoir législatif
PPTX
Socializacion del proyecto de educatic martha reyes
PPT
Taking Away The Keys
PDF
Learning Analytics at KULeuven by the team of Erik Duval
DOCX
C.Harris
PDF
Présentation Schéma Directeur Open Source au S2LQ 2015
PPTX
El nazismo breve resumen
PDF
Safety with a Star (DE)
PPTX
Presentación Gonzalo Cubillos
Ghanshyam Asked To Krishna
Building your-personal-brand-ver-0-7
#Ti2016 Tijuana Innovadora 2016, Creativa
Le collectif du 4 Décembre écrit au pouvoir législatif
Socializacion del proyecto de educatic martha reyes
Taking Away The Keys
Learning Analytics at KULeuven by the team of Erik Duval
C.Harris
Présentation Schéma Directeur Open Source au S2LQ 2015
El nazismo breve resumen
Safety with a Star (DE)
Ad

Similar to jQuery - 10 Time-Savers You (Maybe) Don't Know (20)

PPSX
jQuery - Doing it right
KEY
jQuery: Tips, tricks and hints for better development and Performance
PDF
jQuery quick tips
KEY
jQuery Anti-Patterns for Performance
PDF
fuser interface-development-using-jquery
PPTX
jQuery performance best practices by Sameera Thilakasiri
KEY
jQuery Anti-Patterns for Performance & Compression
PDF
Lotusphere 2012 Speedgeeking - jQuery & Domino, a RAD Combination
KEY
jQuery Performance Tips and Tricks (2011)
KEY
Week 4 - jQuery + Ajax
PPTX
Jquery optimization-tips
PDF
Building a JavaScript Library
PPTX
Jquery fundamentals
PDF
Remy Sharp The DOM scripting toolkit jQuery
KEY
User Interface Development with jQuery
PPT
Jquery Best Practices
PPTX
jQuery
KEY
Bcblackpool jquery tips
PPTX
Unobtrusive javascript with jQuery
PDF
jQuery Training
jQuery - Doing it right
jQuery: Tips, tricks and hints for better development and Performance
jQuery quick tips
jQuery Anti-Patterns for Performance
fuser interface-development-using-jquery
jQuery performance best practices by Sameera Thilakasiri
jQuery Anti-Patterns for Performance & Compression
Lotusphere 2012 Speedgeeking - jQuery & Domino, a RAD Combination
jQuery Performance Tips and Tricks (2011)
Week 4 - jQuery + Ajax
Jquery optimization-tips
Building a JavaScript Library
Jquery fundamentals
Remy Sharp The DOM scripting toolkit jQuery
User Interface Development with jQuery
Jquery Best Practices
jQuery
Bcblackpool jquery tips
Unobtrusive javascript with jQuery
jQuery Training

More from girish82 (7)

PPTX
Introduction to node
PDF
Designing better-ux
PDF
Designing better-ux-workshop-2
PDF
Designing better-ux-workshop-3
PDF
Designing better-ux-workshop-4
PDF
Designing better-ux-workshop-5
PDF
Why we need to hire UX professionals
Introduction to node
Designing better-ux
Designing better-ux-workshop-2
Designing better-ux-workshop-3
Designing better-ux-workshop-4
Designing better-ux-workshop-5
Why we need to hire UX professionals

Recently uploaded (20)

PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Empathic Computing: Creating Shared Understanding
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Electronic commerce courselecture one. Pdf
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
KodekX | Application Modernization Development
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Modernizing your data center with Dell and AMD
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Machine learning based COVID-19 study performance prediction
Chapter 3 Spatial Domain Image Processing.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Encapsulation_ Review paper, used for researhc scholars
Empathic Computing: Creating Shared Understanding
Network Security Unit 5.pdf for BCA BBA.
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Electronic commerce courselecture one. Pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
KodekX | Application Modernization Development
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Spectral efficient network and resource selection model in 5G networks
Mobile App Security Testing_ A Comprehensive Guide.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Unlocking AI with Model Context Protocol (MCP)
Advanced methodologies resolving dimensionality complications for autism neur...
Modernizing your data center with Dell and AMD
Reach Out and Touch Someone: Haptics and Empathic Computing
Machine learning based COVID-19 study performance prediction

jQuery - 10 Time-Savers You (Maybe) Don't Know

  • 1. 10 Time-Savers You (Maybe) Don't Know Girish Gangadharan @appoosa girish@giri.sh http://giri.sh
  • 2. Limit DOM traversal var $items = $(„#products‟); $items.click(function() { $(this).next(„div‟).slideToggle(); }); $items.addClass(„active‟); $items.closest(„p#category‟).animate({ height:100px });
  • 3. Less code. Better readability. var $userprofile = $(„#user-profile‟); $userprofile .hover(function () { $(this).addClass(“highlight"); }, function () { $(this).removeClass(“highlight"); }) .click(function() { //do something }) .fadeIn(„slow‟);
  • 4. Don‟t leave them hanging. :first-child, :last-child, :even, :odd, :parent, :next, :prev, etc. Precede them with a tag name or some selector; else the universal selector is implied. $(':focus') = $('*:focus')
  • 5. Understand what each does. Use appropriately. $('#sometable').each(function(){ $('td', this).bind('hover', function(){ $(this).toggleClass('hover'); }); }); $('#sometable').each(function(){ $('td', this).live('hover', function(){ $(this).toggleClass('hover'); }); }); $('#sometable').delegate('td', 'hover', function(){ $(this).toggleClass('hover'); });
  • 6. Create in memory. Then update the DOM. var $mylist = $('#mylist'); // <ul> for (var i = 0; i < 100; i++) { $mylist.append('<li>' + bestsellers[i] + '</li>'); } var $mylist = $('#mylist'); // <ul> var concatenatedList = “”; for (var i = 0; i < 100; i++) { concatenatedList += ('<li>' + bestsellers[i] + '</li>'); } $mylist.append(concatenatedList);
  • 7. Bind less. $(„#reports td‟).bind(„click‟, function() { //do something }) $(„#reports‟).bind(„click‟, function(e) { var target = e.target; if (target.nodeName === „td') { // do something } })
  • 8. Choose your event carefully. $(document).ready(function() { //Fires as soon as the DOM is ready //Doesn‟t wait for images, style sheets etc. }) $(window).load(function() { //Fires after all the content is loaded //Includes images, style sheets, etc. })
  • 9. Think right-to-left (except for IDs) $('#user-profiles > li > input:disabled'); $('#user-profiles‟).find(„li > input:disabled');
  • 10. Sizzle engine is highly optimized but… var $someElement = $(„#elementId‟); Vs. var someElement = document.getElementById(„elementId‟); $(„#elementId‟).css(„display‟, „block‟); Vs. document.getElementById(„elementId‟).style.display = „block‟; Do you really need a whole library to accomplish your tasks?
  • 11. Will this improve page performance by x times? Probably, not. (especially if you don‟t have a complex multi-nested DOM) For example, how to better structure your code. • Module Pattern • Revealing Module Pattern • Constructional Pattern • Creational Pattern • Factory Pattern • etc.
  • 14. Essential JavaScript Design Patterns: (Free!) http://addyosmani.com/blog/essentialjsdesignpatternsupdate1 jQuery: Novice to Ninja http://www.amazon.com/jQuery-Novice-Ninja-Earle-Castledine/dp/0980576857 jQuery API http://api.jquery.com JavaScript Performance Testing http://jsperf.com/browse
  • 15. 10 Time-Savers You (Maybe) Don't Know Girish Gangadharan @appoosa girish@giri.sh http://giri.sh