Wednesday, May 23, 2012

JQuery - Best Practices

Best practices:
  1. Save selection in a variable if the selection needs to make multiple time.
    ExplanationjQuery doesn't do caching of selections and for every selection lot of code runs behind the scene
  2. Use 'if($("element").length)' instead of  'if($("element"))' to make sure selection has records
    Explanation:  Any selection using $() always returns an object and object always evaluates to TRUE, even if selection doesn't contain any elements.
    'if($("element"))' will return '[]' which is treated as empty object 



1 comment: