Pimp my Ride jQuery
     By @Jack_Franklin
Load from Google
Load from Google
• Google’s CDN allows you to load jQuery:
  <script type="text/javascript" src=http://
  ajax.googleapis.com/ajax/libs/jquery/1/
  jquery.min.js">
Load from Google
• Google’s CDN allows you to load jQuery:
  <script type="text/javascript" src=http://
  ajax.googleapis.com/ajax/libs/jquery/1/
  jquery.min.js">
• Always load most recent version or you
  can be more specific as you wish.
Load from Google
• Google’s CDN allows you to load jQuery:
  <script type="text/javascript" src=http://
  ajax.googleapis.com/ajax/libs/jquery/1/
  jquery.min.js">
• Always load most recent version or you
  can be more specific as you wish.
• Chances are user may already have it
  cached.
Use of CSS SelectorS!
Use of CSS SelectorS!
• jQuery Traversal functions take CSS
  Selector
Use of CSS SelectorS!
• jQuery Traversal functions take CSS
  Selector
• Often forgotten - use multiple selectors to
  make up a CSS selector.
Use of CSS SelectorS!
• jQuery Traversal functions take CSS
  Selector
• Often forgotten - use multiple selectors to
  make up a CSS selector.
• EG: $(elem).siblings(“h2,p,strong”);
Use of CSS SelectorS!
• jQuery Traversal functions take CSS
  Selector
• Often forgotten - use multiple selectors to
  make up a CSS selector.
• EG: $(elem).siblings(“h2,p,strong”);
• Be aware of the efficiency.
Save Selectors
Save Selectors
• Urrgh:!
  $(“myelem”).hide();
  $(“myelem”).css(“width”, ”200”);
  $(“myelem”).show();
Save Selectors
• Urrgh:!
  $(“myelem”).hide();
  $(“myelem”).css(“width”, ”200”);
  $(“myelem”).show();
• Nicer:
  var elem = $(“myelem”);
  elem.hide();
  elem.css(“width”, “200”);
  elem.show();
Or Chain
Or Chain

• $(“myelem”).hide().css(“width”,
  “200”).show();
Or Chain

• $(“myelem”).hide().css(“width”,
  “200”).show();
• Either use this or caching to variables -
  whichever suits you and/or the situation
  best.
Chain over Multiple
       Lines
Chain over Multiple
         Lines
• $(“elem”).hide().addClass().show().attr();
Chain over Multiple
         Lines
• $(“elem”).hide().addClass().show().attr();
• $(“elem”)
     .hide()
     .addClass()
  //etc
Context
Context

• If you’ve cached an element, you can add
  this as context as the second parameter:
  $(“myelem”, someCachedElement);
Context

• If you’ve cached an element, you can add
  this as context as the second parameter:
  $(“myelem”, someCachedElement);
• This looks for “myelem” within
  someCachedElement.
Context Continued
Context Continued
• Contextualised selectors should be in the
  form:
  $(selector, context)
Context Continued
• Contextualised selectors should be in the
  form:
  $(selector, context)
• EG:
  $(“myelem”, “#somediv”);
  jQuery will only search inside #somediv for
  “myelem”.
Context Continued
Context Continued
• When you do use the context, jQuery just
  calls the find() method on that context:
  $(selector, context) = $
  (context).find(selector).
Context Continued
• When you do use the context, jQuery just
  calls the find() method on that context:
  $(selector, context) = $
  (context).find(selector).
• So the best way is to skip that step and just
  do:
  $(context).find(selector);
Clever Traversing




  http://api.jquery.com/category/traversing/
Clever Traversing
• jQuery provides so many traversing
  functions that there is often a shortcut.




          http://api.jquery.com/category/traversing/
Clever Traversing
• jQuery provides so many traversing
  functions that there is often a shortcut.
• Rarely should you have to go back to
  where you’ve gone:
  $
  ("elem").find("p").hide().parent().find("h2").h
  ide();


          http://api.jquery.com/category/traversing/
Clever Traversing
• jQuery provides so many traversing
  functions that there is often a shortcut.
• Rarely should you have to go back to
  where you’ve gone:
  $
  ("elem").find("p").hide().parent().find("h2").h
  ide();
• Can easily be made much nicer:
          http://api.jquery.com/category/traversing/
Clever Traversing
Clever Traversing

• Be sensible - be sure to use the right
  function for the right job.
Clever Traversing

• Be sensible - be sure to use the right
  function for the right job.
• For example - the functions find() and
  children().
Avoid Class Selection




 http://www.quirksmode.org/dom/w3c_core.html
Avoid Class Selection

• getElementById and
  getElementsByTagName are preferred to
  getElementsByClassName - supported in all
  browsers bar IE6, 7 and 8 (surprise?).




  http://www.quirksmode.org/dom/w3c_core.html
Avoid Class Selection

• getElementById and
  getElementsByTagName are preferred to
  getElementsByClassName - supported in all
  browsers bar IE6, 7 and 8 (surprise?).
• As jQuery can rely on getElementById &
  getElementsByTagName select by ID or Tag
  if you possibly can.

   http://www.quirksmode.org/dom/w3c_core.html
Check if an Element
      Exists
Check if an Element
        Exists
• Easy:
  if($(“elem”).length) {
  //element must exist
  }
Check if an Element
        Exists
• Easy:
  if($(“elem”).length) {
  //element must exist
  }
• jQuery gracefully degrades - if it does not
  exist no errors thrown and nothing breaks.
Run onLoad
Run onLoad
• Run your Javascript once the DOM is
  loaded like so (when using jQuery):
  $(document).ready(function() {
  //add code in now
  }
Run onLoad
• Run your Javascript once the DOM is
  loaded like so (when using jQuery):
  $(document).ready(function() {
  //add code in now
  }
• Shorten this to:
  $(function() {
  //code here
  }
Apply a Class to
   <body>
Apply a Class to
         <body>
• The first line of your code can add a class
  to the body to inform you if Javascript is
  on:
  $(body).addClass(“javascripton”);
Apply a Class to
         <body>
• The first line of your code can add a class
  to the body to inform you if Javascript is
  on:
  $(body).addClass(“javascripton”);
• On a similar note, if you can make CSS do
  the heavy work, do so. Javascript should do
  the bare minimum.
Store info with
jQuery’s data method
Store info with
jQuery’s data method
• jQuery provides its own method for
  storing data to be used later on.
  $(“myelem”).data(“usefulstuff”,”value of
  this data”)
Store info with
jQuery’s data method
• jQuery provides its own method for
  storing data to be used later on.
  $(“myelem”).data(“usefulstuff”,”value of
  this data”)
• And it can be retrieved like so:
  $(“myelem”).data(“usefulstuff”);
Surrender the $
Surrender the $
• jQuery.noConflict();
  Now use jQuery instead of $:
  jQuery(“myelem”).hide();
Surrender the $
• jQuery.noConflict();
  Now use jQuery instead of $:
  jQuery(“myelem”).hide();
• (function($) {
  //use $ as normal here for jQuery
  })(jQuery);
  //out here $ is not relating to jQuery
Combine & Minify


• Once you’re done, combining your scripts
  into one and then minifying them is a big
  help.
Combine & Minify


• Once you’re done, combining your scripts
  into one and then minifying them is a big
  help.
      http://www.scriptalizer.com/
Links & Resources

• Official site: www.jquery.com
• jQuery Docs: docs.jquery.com
• www.learningjquery.com
• net.tutsplus.com (hit and miss)
Thanks!
  @Jack_Franklin
www.jackfranklin.co.uk

Bcblackpool jquery tips

  • 1.
    Pimp my RidejQuery By @Jack_Franklin
  • 2.
  • 3.
    Load from Google •Google’s CDN allows you to load jQuery: <script type="text/javascript" src=http:// ajax.googleapis.com/ajax/libs/jquery/1/ jquery.min.js">
  • 4.
    Load from Google •Google’s CDN allows you to load jQuery: <script type="text/javascript" src=http:// ajax.googleapis.com/ajax/libs/jquery/1/ jquery.min.js"> • Always load most recent version or you can be more specific as you wish.
  • 5.
    Load from Google •Google’s CDN allows you to load jQuery: <script type="text/javascript" src=http:// ajax.googleapis.com/ajax/libs/jquery/1/ jquery.min.js"> • Always load most recent version or you can be more specific as you wish. • Chances are user may already have it cached.
  • 6.
    Use of CSSSelectorS!
  • 7.
    Use of CSSSelectorS! • jQuery Traversal functions take CSS Selector
  • 8.
    Use of CSSSelectorS! • jQuery Traversal functions take CSS Selector • Often forgotten - use multiple selectors to make up a CSS selector.
  • 9.
    Use of CSSSelectorS! • jQuery Traversal functions take CSS Selector • Often forgotten - use multiple selectors to make up a CSS selector. • EG: $(elem).siblings(“h2,p,strong”);
  • 10.
    Use of CSSSelectorS! • jQuery Traversal functions take CSS Selector • Often forgotten - use multiple selectors to make up a CSS selector. • EG: $(elem).siblings(“h2,p,strong”); • Be aware of the efficiency.
  • 11.
  • 12.
    Save Selectors • Urrgh:! $(“myelem”).hide(); $(“myelem”).css(“width”, ”200”); $(“myelem”).show();
  • 13.
    Save Selectors • Urrgh:! $(“myelem”).hide(); $(“myelem”).css(“width”, ”200”); $(“myelem”).show(); • Nicer: var elem = $(“myelem”); elem.hide(); elem.css(“width”, “200”); elem.show();
  • 14.
  • 15.
  • 16.
    Or Chain • $(“myelem”).hide().css(“width”, “200”).show(); • Either use this or caching to variables - whichever suits you and/or the situation best.
  • 17.
  • 18.
    Chain over Multiple Lines • $(“elem”).hide().addClass().show().attr();
  • 19.
    Chain over Multiple Lines • $(“elem”).hide().addClass().show().attr(); • $(“elem”) .hide() .addClass() //etc
  • 20.
  • 21.
    Context • If you’vecached an element, you can add this as context as the second parameter: $(“myelem”, someCachedElement);
  • 22.
    Context • If you’vecached an element, you can add this as context as the second parameter: $(“myelem”, someCachedElement); • This looks for “myelem” within someCachedElement.
  • 23.
  • 24.
    Context Continued • Contextualisedselectors should be in the form: $(selector, context)
  • 25.
    Context Continued • Contextualisedselectors should be in the form: $(selector, context) • EG: $(“myelem”, “#somediv”); jQuery will only search inside #somediv for “myelem”.
  • 26.
  • 27.
    Context Continued • Whenyou do use the context, jQuery just calls the find() method on that context: $(selector, context) = $ (context).find(selector).
  • 28.
    Context Continued • Whenyou do use the context, jQuery just calls the find() method on that context: $(selector, context) = $ (context).find(selector). • So the best way is to skip that step and just do: $(context).find(selector);
  • 29.
    Clever Traversing http://api.jquery.com/category/traversing/
  • 30.
    Clever Traversing • jQueryprovides so many traversing functions that there is often a shortcut. http://api.jquery.com/category/traversing/
  • 31.
    Clever Traversing • jQueryprovides so many traversing functions that there is often a shortcut. • Rarely should you have to go back to where you’ve gone: $ ("elem").find("p").hide().parent().find("h2").h ide(); http://api.jquery.com/category/traversing/
  • 32.
    Clever Traversing • jQueryprovides so many traversing functions that there is often a shortcut. • Rarely should you have to go back to where you’ve gone: $ ("elem").find("p").hide().parent().find("h2").h ide(); • Can easily be made much nicer: http://api.jquery.com/category/traversing/
  • 33.
  • 34.
    Clever Traversing • Besensible - be sure to use the right function for the right job.
  • 35.
    Clever Traversing • Besensible - be sure to use the right function for the right job. • For example - the functions find() and children().
  • 36.
    Avoid Class Selection http://www.quirksmode.org/dom/w3c_core.html
  • 37.
    Avoid Class Selection •getElementById and getElementsByTagName are preferred to getElementsByClassName - supported in all browsers bar IE6, 7 and 8 (surprise?). http://www.quirksmode.org/dom/w3c_core.html
  • 38.
    Avoid Class Selection •getElementById and getElementsByTagName are preferred to getElementsByClassName - supported in all browsers bar IE6, 7 and 8 (surprise?). • As jQuery can rely on getElementById & getElementsByTagName select by ID or Tag if you possibly can. http://www.quirksmode.org/dom/w3c_core.html
  • 39.
    Check if anElement Exists
  • 40.
    Check if anElement Exists • Easy: if($(“elem”).length) { //element must exist }
  • 41.
    Check if anElement Exists • Easy: if($(“elem”).length) { //element must exist } • jQuery gracefully degrades - if it does not exist no errors thrown and nothing breaks.
  • 42.
  • 43.
    Run onLoad • Runyour Javascript once the DOM is loaded like so (when using jQuery): $(document).ready(function() { //add code in now }
  • 44.
    Run onLoad • Runyour Javascript once the DOM is loaded like so (when using jQuery): $(document).ready(function() { //add code in now } • Shorten this to: $(function() { //code here }
  • 45.
    Apply a Classto <body>
  • 46.
    Apply a Classto <body> • The first line of your code can add a class to the body to inform you if Javascript is on: $(body).addClass(“javascripton”);
  • 47.
    Apply a Classto <body> • The first line of your code can add a class to the body to inform you if Javascript is on: $(body).addClass(“javascripton”); • On a similar note, if you can make CSS do the heavy work, do so. Javascript should do the bare minimum.
  • 48.
  • 49.
    Store info with jQuery’sdata method • jQuery provides its own method for storing data to be used later on. $(“myelem”).data(“usefulstuff”,”value of this data”)
  • 50.
    Store info with jQuery’sdata method • jQuery provides its own method for storing data to be used later on. $(“myelem”).data(“usefulstuff”,”value of this data”) • And it can be retrieved like so: $(“myelem”).data(“usefulstuff”);
  • 51.
  • 52.
    Surrender the $ •jQuery.noConflict(); Now use jQuery instead of $: jQuery(“myelem”).hide();
  • 53.
    Surrender the $ •jQuery.noConflict(); Now use jQuery instead of $: jQuery(“myelem”).hide(); • (function($) { //use $ as normal here for jQuery })(jQuery); //out here $ is not relating to jQuery
  • 54.
    Combine & Minify •Once you’re done, combining your scripts into one and then minifying them is a big help.
  • 55.
    Combine & Minify •Once you’re done, combining your scripts into one and then minifying them is a big help. http://www.scriptalizer.com/
  • 56.
    Links & Resources •Official site: www.jquery.com • jQuery Docs: docs.jquery.com • www.learningjquery.com • net.tutsplus.com (hit and miss)
  • 57.

Editor's Notes

  • #2 Point out that it&amp;#x2019;s mainly for the beginners/intermediate users. Feel free to throw in your own tips at the end.
  • #6 Most jQuery functions take the form $(something).function(selector) This selector can be any CSS one - including CSS3. Works in multiple browsers.
  • #7 Most jQuery functions take the form $(something).function(selector) This selector can be any CSS one - including CSS3. Works in multiple browsers.
  • #8 Most jQuery functions take the form $(something).function(selector) This selector can be any CSS one - including CSS3. Works in multiple browsers.
  • #9 Most jQuery functions take the form $(something).function(selector) This selector can be any CSS one - including CSS3. Works in multiple browsers.
  • #10 Some people like to append the dollar sign to variables that have the jQuery object &amp;#x201C;wrapped&amp;#x201D; around them.
  • #11 Some people like to append the dollar sign to variables that have the jQuery object &amp;#x201C;wrapped&amp;#x201D; around them.
  • #12 If you&amp;#x2019;re only performing multiple operations on one selector in one place, chaining is probably best. However if you plan to reuse the selector much later then cache it.
  • #13 If you&amp;#x2019;re only performing multiple operations on one selector in one place, chaining is probably best. However if you plan to reuse the selector much later then cache it.
  • #14 Completely optional - your call
  • #15 Completely optional - your call
  • #16 Just continuing from caching
  • #17 Just continuing from caching
  • #18 End with however...
  • #19 End with however...
  • #20 You can use the $(selector,context) method or .find(), choice is yours. Again, there are so many different ways of achieving the same result with jQuery
  • #21 You can use the $(selector,context) method or .find(), choice is yours. Again, there are so many different ways of achieving the same result with jQuery
  • #22 Browse the traversing docs - URL there - so many functions.
  • #23 Browse the traversing docs - URL there - so many functions.
  • #24 Browse the traversing docs - URL there - so many functions.
  • #25 find() will descend right through the DOM children() only descends one level
  • #26 find() will descend right through the DOM children() only descends one level
  • #27 getElementsByClassName does currently work in IE9 preview, but not in its predecessors.
  • #28 getElementsByClassName does currently work in IE9 preview, but not in its predecessors.
  • #31 Or, you can put all your jQuery at the bottom, just before the closing &lt;/body&gt; tag and not have to use this code.
  • #32 Or, you can put all your jQuery at the bottom, just before the closing &lt;/body&gt; tag and not have to use this code.
  • #33 Easy quick way to detect if javascript is on. (Most people do have it on)
  • #34 Easy quick way to detect if javascript is on. (Most people do have it on)
  • #35 Just an easy way to store things - seen people store things in alt tags, classes, etc, this is much easier.
  • #36 Just an easy way to store things - seen people store things in alt tags, classes, etc, this is much easier.
  • #37 For use with other libraries
  • #38 For use with other libraries
  • #40 A quick google search for tutorials finds you loads.