Differentiate between $() vs. querySelectorAll() in jQuery

The $() function is similar to the Document method querySelectorAll(): both take a CSS selector as their argument and return an array-like object that holds the elements that match the selector.

The jQuery implementation uses querySelectorAll() in browsers that support it, but there are good reasons to use $() instead of querySelectorAll() in your own code:
  • querySelectorAll() has only recently been implemented by browser vendors, whereas $() works in older browsers as well as new ones.
  • Because jQuery can perform selections “by hand”, the CSS3 selectors supported by $() work in all browsers, not just those browsers that support CSS3.
  • The array-like object returned by $() (a jQuery object) is much more useful than the array-like object (a NodeList) returned by querySelectorAll().

No comments:

Post a Comment