SlideShare a Scribd company logo
1 of 29
Presentational jQuery
                        Balancing jQuery and CSS




Presentational jQuery                              Doug Neiner
Presentational jQuery   Doug Neiner
Presentational jQuery   Doug Neiner
Presentational jQuery
                        Balancing jQuery and CSS




Presentational jQuery                              Doug Neiner
Presentational jQuery
 • jQuery          CSS Primer

 • .css()       vs. Style Sheets vs. <style> blocks

 • Rules         of the Road
                        Balancing jQuery and CSS

 • Moving               Around




Presentational jQuery                                 Doug Neiner
jQuery CSS Primer
 • jQuery adjusts the .style property on the DOM
    element to make CSS changes
    div.style.backgroundColor = "#aaa";




Presentational jQuery                              Doug Neiner
jQuery CSS Primer
   var div = document.getElementById( "#test" );

   // JS
   div.style.backgroundColor = "#aaa";

   // jQuery
   $(div).css( "backgroundColor", "#aaa" );
   $(div).css( "background-color", "#aaa" );

   // JS
   div.style.color = "#000";
   div.style.textDecoration = "underline";

   // jQuery
   $(div).css({
     color: "#000",
     textDecoration: "underline"
   });


Presentational jQuery                              Doug Neiner
jQuery CSS Primer
 • jQuery adjusts the            .style   property on the DOM
    element
    div.style.backgroundColor = "#aaa";


 • It   is the equivalent of setting an inline style
    <div style="background-color: #aaa"> … </div>


 • This  overrides pretty much any rule specified
    elsewhere




Presentational jQuery                                           Doug Neiner
Getting CSS Values
   // Get the value for #test
   $( "#test" ).css( "border-top-width" );

   // Get the value for the first item in the result set
   $( "div" ).css( "border-top-width" );

   // Get all the values in the result set
   var values = $( "div" ).map( function () {
     return $(this).css( "border-top-width" );
   }).get();




Presentational jQuery                                     Doug Neiner
Dynamic Setters
   // Get the value for #test
   $( "div" ).css( "border-top-width", function ( i, old ) {
      return ( i + 1 ) * 2; // New Value
   });




Presentational jQuery                                          Doug Neiner
.css() vs. Style Sheets vs.
                         <style>
                    Choosing the Best Tool for the Task




Presentational jQuery                                     Doug Neiner
Style Sheet
 • Pros
     •   Very fast
     •   Can be easily overridden
     •   Provides a customized foundation
     •   Clear separation of logic and style

 • Cons
     •   You must know the element states before hand
     •   You must be able to select the elements
     •   Is not (generally) reactive

Presentational jQuery                                   Doug Neiner
<style> Block
 • Pros
     •   Very fast
     •   Can be overridden
     •   Adds to a foundation, or provides its own
     •   Can be reactive

 • Cons
     •   Less separation of logic and style
     •   You must be able to select the elements



Presentational jQuery                                Doug Neiner
.css() Method
 • Pros
     •   Very convenient
     •   Highly dynamic and reactive
     •   Can act on an arbitrary selection of elements

 • Cons
     •   Not easily overridden
     •   No separation of logic and style




Presentational jQuery                                    Doug Neiner
Style Sheet                 <style>                jQuery
 • To  set initial      • BulkChanges       • Totransition
    state                to elements         between
                                             states
 • To  handle           • Defaults   that
    predictable          can be             • Tohandle
    states               overridden          unpredictable
                                             states
 • Bulk  changes
    to elements                             • One and two-
                                             off changes
                                             to elements


Presentational jQuery                                 Doug Neiner
A note to plugin developers
 • If    you need a lot of styles, do it in a stylesheet
     •   No asset path issues
     •   Easily customized
     •   Nice separation of logic and style

 • Ifyou have to do it only in JS, prepend it to the
    <head> in a <style> block.




Presentational jQuery                                  Doug Neiner
Rules of the Road
                        Avoiding Common Mistakes




Presentational jQuery                              Doug Neiner
Beware of Iteration
           Don't call .css() multiple times unless needed




Presentational jQuery                                   Doug Neiner
Incorrect Approach
   var $div = $("div");

   $div.css('backgroundColor', 'red');

   if ( some_test === true ) {
     $div.css('color', 'black');
   }

   ...

   if ( some_other_test === true ) {
     $div.css('display', 'block')
         .css('position', 'relative');
   }




Presentational jQuery                    Doug Neiner
Correct Approach
   var css = {
     backgroundColor: "red"
   };

   if ( some_test === true ) {
     css.color = "black";
   }

   ...

   if ( some_other_test === true ) {
     css.display = "block";
     css.position = "relative";
   }

   $("div").css( css );




Presentational jQuery                  Doug Neiner
Incorrect Approach
   var colors = $(".color");

   $(".filter-by-color").click( function () {
      colors.toggle();
   });




Presentational jQuery                          Doug Neiner
Correct Approach
   var color_parent = $("#list");

   $(".filter-by-color").click( function () {
      color_parent.toggleClass( "show-colors" );
   });



   .color { display: none }

   #list.show-colors .color { display: block }




Presentational jQuery                              Doug Neiner
Class Methods
 • addClass(            classNames )

 • removeClass(            classNames )

 • toggleClass(           classNames, is_true )

 • hasClass(            className )

 • is(    ".className1.className2")




Presentational jQuery                             Doug Neiner
Write code like you
                            run errands
                    Don't keep revisiting the same store
                             on the same day




Presentational jQuery                                      Doug Neiner
Setting Initial State
                             To js or no-js




Presentational jQuery                           Doug Neiner
Incorrect Approach

   $( document ).ready( function () {
      $( "#dialog, #menu, #footer" ).hide();
      $( "#progress-bar" ).show();
   });




Presentational jQuery                          Doug Neiner
Correct Approach
   <html class="no-js">
   …
   <script>
   document.documentElement.className =
     document.documentElement.className.replace("no-js", "js");
   </script>


   #dialog, #menu, #footer { display: block }

   .no-js #progress-bar,
   .js #dialog, .js #menu, .js #footer { display: none }




                          Modernizer does this for you


Presentational jQuery                                             Doug Neiner
Moving Around
                          jQuery Animation

                                                                   . I will
                                                         ex ercise
                                                   e coding
                                           s a liv                rial on
                                     on wa           e
                         Thi s secti               b        e mate g, and
                                                     ng som queuin
                                             blishi thod,
                                       on pu n me
                            w orking nimatio
                                    's A             sing.
                           jQuery                ea


Presentational jQuery                                              Doug Neiner
twitter   @dougneiner

                         email    doug@dougneiner.com

                          web     http://dougneiner.com




Presentational jQuery                                     Doug Neiner

More Related Content

What's hot

How I Learned to Stop Worrying and Love jQuery (Jan 2013)
How I Learned to Stop Worrying and Love jQuery (Jan 2013)How I Learned to Stop Worrying and Love jQuery (Jan 2013)
How I Learned to Stop Worrying and Love jQuery (Jan 2013)
David Giard
 
J query presentation
J query presentationJ query presentation
J query presentation
akanksha17
 
New text documentfsdfs
New text documentfsdfsNew text documentfsdfs
New text documentfsdfs
Ah Lom
 

What's hot (19)

JsViews - Next Generation jQuery Templates
JsViews - Next Generation jQuery TemplatesJsViews - Next Generation jQuery Templates
JsViews - Next Generation jQuery Templates
 
JavaScript Libraries (@Media)
JavaScript Libraries (@Media)JavaScript Libraries (@Media)
JavaScript Libraries (@Media)
 
JavaScript Libraries (Kings of Code)
JavaScript Libraries (Kings of Code)JavaScript Libraries (Kings of Code)
JavaScript Libraries (Kings of Code)
 
Getting started with jQuery
Getting started with jQueryGetting started with jQuery
Getting started with jQuery
 
J query
J queryJ query
J query
 
jQuery (MeshU)
jQuery (MeshU)jQuery (MeshU)
jQuery (MeshU)
 
DirectToWeb 2.0
DirectToWeb 2.0DirectToWeb 2.0
DirectToWeb 2.0
 
How I Learned to Stop Worrying and Love jQuery (Jan 2013)
How I Learned to Stop Worrying and Love jQuery (Jan 2013)How I Learned to Stop Worrying and Love jQuery (Jan 2013)
How I Learned to Stop Worrying and Love jQuery (Jan 2013)
 
jQuery - write less, do more javascript toolkit
jQuery - write less, do more javascript toolkit jQuery - write less, do more javascript toolkit
jQuery - write less, do more javascript toolkit
 
JQuery
JQueryJQuery
JQuery
 
J query presentation
J query presentationJ query presentation
J query presentation
 
State of jQuery and Drupal
State of jQuery and DrupalState of jQuery and Drupal
State of jQuery and Drupal
 
New text documentfsdfs
New text documentfsdfsNew text documentfsdfs
New text documentfsdfs
 
jQuery For Beginners - jQuery Conference 2009
jQuery For Beginners - jQuery Conference 2009jQuery For Beginners - jQuery Conference 2009
jQuery For Beginners - jQuery Conference 2009
 
ME vs WEB - AngularJS Fundamentals
ME vs WEB - AngularJS FundamentalsME vs WEB - AngularJS Fundamentals
ME vs WEB - AngularJS Fundamentals
 
jQuery (BostonPHP)
jQuery (BostonPHP)jQuery (BostonPHP)
jQuery (BostonPHP)
 
Pocket Knife JS
Pocket Knife JSPocket Knife JS
Pocket Knife JS
 
jQuery (DrupalCamp Toronto)
jQuery (DrupalCamp Toronto)jQuery (DrupalCamp Toronto)
jQuery (DrupalCamp Toronto)
 
jQuery Conference Chicago - September 2014
jQuery Conference Chicago - September 2014jQuery Conference Chicago - September 2014
jQuery Conference Chicago - September 2014
 

Viewers also liked

Introducing Amplify
Introducing AmplifyIntroducing Amplify
Introducing Amplify
appendTo
 
HTML5 and CSS3 Techniques You Can Use Today
HTML5 and CSS3 Techniques You Can Use TodayHTML5 and CSS3 Techniques You Can Use Today
HTML5 and CSS3 Techniques You Can Use Today
Todd Anglin
 
Ppt of web development
Ppt of web developmentPpt of web development
Ppt of web development
bethanygfair
 

Viewers also liked (8)

Introducing Amplify
Introducing AmplifyIntroducing Amplify
Introducing Amplify
 
HTML5 and CSS3 Techniques You Can Use Today
HTML5 and CSS3 Techniques You Can Use TodayHTML5 and CSS3 Techniques You Can Use Today
HTML5 and CSS3 Techniques You Can Use Today
 
Object Oriented CSS
Object Oriented CSSObject Oriented CSS
Object Oriented CSS
 
Fundamentals of Web for Non-Developers
Fundamentals of Web for Non-DevelopersFundamentals of Web for Non-Developers
Fundamentals of Web for Non-Developers
 
Basic Web Concepts
Basic Web ConceptsBasic Web Concepts
Basic Web Concepts
 
Ppt of web development
Ppt of web developmentPpt of web development
Ppt of web development
 
Fundamentals of Web Development For Non-Developers
Fundamentals of Web Development For Non-DevelopersFundamentals of Web Development For Non-Developers
Fundamentals of Web Development For Non-Developers
 
CSS101 - Concept Fundamentals for non UI Developers
CSS101 - Concept Fundamentals for non UI DevelopersCSS101 - Concept Fundamentals for non UI Developers
CSS101 - Concept Fundamentals for non UI Developers
 

Similar to Presentational jQuery

Learning jquery-in-30-minutes-1195942580702664-3
Learning jquery-in-30-minutes-1195942580702664-3Learning jquery-in-30-minutes-1195942580702664-3
Learning jquery-in-30-minutes-1195942580702664-3
luckysb16
 
Intro to jQuery for Drupal
Intro to jQuery for DrupalIntro to jQuery for Drupal
Intro to jQuery for Drupal
jhamiltoorion
 
PHP Development With MongoDB
PHP Development With MongoDBPHP Development With MongoDB
PHP Development With MongoDB
Fitz Agard
 
PHP Development with MongoDB (Fitz Agard)
PHP Development with MongoDB (Fitz Agard)PHP Development with MongoDB (Fitz Agard)
PHP Development with MongoDB (Fitz Agard)
MongoSF
 
Javascript Libraries
Javascript LibrariesJavascript Libraries
Javascript Libraries
elliando dias
 

Similar to Presentational jQuery (20)

Jquery
JqueryJquery
Jquery
 
jQuery: Nuts, Bolts and Bling
jQuery: Nuts, Bolts and BlingjQuery: Nuts, Bolts and Bling
jQuery: Nuts, Bolts and Bling
 
Lotusphere 2012 Speedgeeking - jQuery & Domino, a RAD Combination
Lotusphere 2012 Speedgeeking - jQuery & Domino, a RAD CombinationLotusphere 2012 Speedgeeking - jQuery & Domino, a RAD Combination
Lotusphere 2012 Speedgeeking - jQuery & Domino, a RAD Combination
 
Awesome html with ujs, jQuery and coffeescript
Awesome html with ujs, jQuery and coffeescriptAwesome html with ujs, jQuery and coffeescript
Awesome html with ujs, jQuery and coffeescript
 
Jquery News Packages
Jquery News PackagesJquery News Packages
Jquery News Packages
 
Learning jquery-in-30-minutes-1195942580702664-3
Learning jquery-in-30-minutes-1195942580702664-3Learning jquery-in-30-minutes-1195942580702664-3
Learning jquery-in-30-minutes-1195942580702664-3
 
jQuery for beginners
jQuery for beginnersjQuery for beginners
jQuery for beginners
 
Learning jQuery in 30 minutes
Learning jQuery in 30 minutesLearning jQuery in 30 minutes
Learning jQuery in 30 minutes
 
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)
 
JavaScript the Smart Way - Getting Started with jQuery
JavaScript the Smart Way - Getting Started with jQueryJavaScript the Smart Way - Getting Started with jQuery
JavaScript the Smart Way - Getting Started with jQuery
 
Intro to jQuery for Drupal
Intro to jQuery for DrupalIntro to jQuery for Drupal
Intro to jQuery for Drupal
 
jQuery Loves Developers - Oredev 2009
jQuery Loves Developers - Oredev 2009jQuery Loves Developers - Oredev 2009
jQuery Loves Developers - Oredev 2009
 
PHP Development With MongoDB
PHP Development With MongoDBPHP Development With MongoDB
PHP Development With MongoDB
 
PHP Development with MongoDB (Fitz Agard)
PHP Development with MongoDB (Fitz Agard)PHP Development with MongoDB (Fitz Agard)
PHP Development with MongoDB (Fitz Agard)
 
Write Less Do More
Write Less Do MoreWrite Less Do More
Write Less Do More
 
Tips for writing Javascript for Drupal
Tips for writing Javascript for DrupalTips for writing Javascript for Drupal
Tips for writing Javascript for Drupal
 
Javascript Libraries
Javascript LibrariesJavascript Libraries
Javascript Libraries
 
Jquery
JqueryJquery
Jquery
 
Inside jQuery (2011)
Inside jQuery (2011)Inside jQuery (2011)
Inside jQuery (2011)
 
Dojo Confessions
Dojo ConfessionsDojo Confessions
Dojo Confessions
 

Recently uploaded

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Recently uploaded (20)

TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 

Presentational jQuery

  • 1. Presentational jQuery Balancing jQuery and CSS Presentational jQuery Doug Neiner
  • 2. Presentational jQuery Doug Neiner
  • 3. Presentational jQuery Doug Neiner
  • 4. Presentational jQuery Balancing jQuery and CSS Presentational jQuery Doug Neiner
  • 5. Presentational jQuery • jQuery CSS Primer • .css() vs. Style Sheets vs. <style> blocks • Rules of the Road Balancing jQuery and CSS • Moving Around Presentational jQuery Doug Neiner
  • 6. jQuery CSS Primer • jQuery adjusts the .style property on the DOM element to make CSS changes div.style.backgroundColor = "#aaa"; Presentational jQuery Doug Neiner
  • 7. jQuery CSS Primer var div = document.getElementById( "#test" ); // JS div.style.backgroundColor = "#aaa"; // jQuery $(div).css( "backgroundColor", "#aaa" ); $(div).css( "background-color", "#aaa" ); // JS div.style.color = "#000"; div.style.textDecoration = "underline"; // jQuery $(div).css({ color: "#000", textDecoration: "underline" }); Presentational jQuery Doug Neiner
  • 8. jQuery CSS Primer • jQuery adjusts the .style property on the DOM element div.style.backgroundColor = "#aaa"; • It is the equivalent of setting an inline style <div style="background-color: #aaa"> … </div> • This overrides pretty much any rule specified elsewhere Presentational jQuery Doug Neiner
  • 9. Getting CSS Values // Get the value for #test $( "#test" ).css( "border-top-width" ); // Get the value for the first item in the result set $( "div" ).css( "border-top-width" ); // Get all the values in the result set var values = $( "div" ).map( function () { return $(this).css( "border-top-width" ); }).get(); Presentational jQuery Doug Neiner
  • 10. Dynamic Setters // Get the value for #test $( "div" ).css( "border-top-width", function ( i, old ) { return ( i + 1 ) * 2; // New Value }); Presentational jQuery Doug Neiner
  • 11. .css() vs. Style Sheets vs. <style> Choosing the Best Tool for the Task Presentational jQuery Doug Neiner
  • 12. Style Sheet • Pros • Very fast • Can be easily overridden • Provides a customized foundation • Clear separation of logic and style • Cons • You must know the element states before hand • You must be able to select the elements • Is not (generally) reactive Presentational jQuery Doug Neiner
  • 13. <style> Block • Pros • Very fast • Can be overridden • Adds to a foundation, or provides its own • Can be reactive • Cons • Less separation of logic and style • You must be able to select the elements Presentational jQuery Doug Neiner
  • 14. .css() Method • Pros • Very convenient • Highly dynamic and reactive • Can act on an arbitrary selection of elements • Cons • Not easily overridden • No separation of logic and style Presentational jQuery Doug Neiner
  • 15. Style Sheet <style> jQuery • To set initial • BulkChanges • Totransition state to elements between states • To handle • Defaults that predictable can be • Tohandle states overridden unpredictable states • Bulk changes to elements • One and two- off changes to elements Presentational jQuery Doug Neiner
  • 16. A note to plugin developers • If you need a lot of styles, do it in a stylesheet • No asset path issues • Easily customized • Nice separation of logic and style • Ifyou have to do it only in JS, prepend it to the <head> in a <style> block. Presentational jQuery Doug Neiner
  • 17. Rules of the Road Avoiding Common Mistakes Presentational jQuery Doug Neiner
  • 18. Beware of Iteration Don't call .css() multiple times unless needed Presentational jQuery Doug Neiner
  • 19. Incorrect Approach var $div = $("div"); $div.css('backgroundColor', 'red'); if ( some_test === true ) { $div.css('color', 'black'); } ... if ( some_other_test === true ) { $div.css('display', 'block') .css('position', 'relative'); } Presentational jQuery Doug Neiner
  • 20. Correct Approach var css = { backgroundColor: "red" }; if ( some_test === true ) { css.color = "black"; } ... if ( some_other_test === true ) { css.display = "block"; css.position = "relative"; } $("div").css( css ); Presentational jQuery Doug Neiner
  • 21. Incorrect Approach var colors = $(".color"); $(".filter-by-color").click( function () { colors.toggle(); }); Presentational jQuery Doug Neiner
  • 22. Correct Approach var color_parent = $("#list"); $(".filter-by-color").click( function () { color_parent.toggleClass( "show-colors" ); }); .color { display: none } #list.show-colors .color { display: block } Presentational jQuery Doug Neiner
  • 23. Class Methods • addClass( classNames ) • removeClass( classNames ) • toggleClass( classNames, is_true ) • hasClass( className ) • is( ".className1.className2") Presentational jQuery Doug Neiner
  • 24. Write code like you run errands Don't keep revisiting the same store on the same day Presentational jQuery Doug Neiner
  • 25. Setting Initial State To js or no-js Presentational jQuery Doug Neiner
  • 26. Incorrect Approach $( document ).ready( function () { $( "#dialog, #menu, #footer" ).hide(); $( "#progress-bar" ).show(); }); Presentational jQuery Doug Neiner
  • 27. Correct Approach <html class="no-js"> … <script> document.documentElement.className = document.documentElement.className.replace("no-js", "js"); </script> #dialog, #menu, #footer { display: block } .no-js #progress-bar, .js #dialog, .js #menu, .js #footer { display: none } Modernizer does this for you Presentational jQuery Doug Neiner
  • 28. Moving Around jQuery Animation . I will ex ercise e coding s a liv rial on on wa e Thi s secti b e mate g, and ng som queuin blishi thod, on pu n me w orking nimatio 's A sing. jQuery ea Presentational jQuery Doug Neiner
  • 29. twitter @dougneiner email doug@dougneiner.com web http://dougneiner.com Presentational jQuery Doug Neiner

Editor's Notes

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n