logo

drewdevault.com

[mirror] blog and personal website of Drew DeVault git clone https://hacktivis.me/git/mirror/drewdevault.com.git

You-dont-need-jQuery.md (5249B)


  1. ---
  2. date: 2013-08-19
  3. title: You don't need jQuery
  4. layout: post
  5. tags: [javascript]
  6. ---
  7. It's true. You really don't need jQuery. Modern web browsers can do most of what you want from jQuery,
  8. without jQuery.
  9. For example, take [MediaCrush](https://mediacru.sh). It's a website I spent some time working on with a friend.
  10. It's actually quite sophisticated - drag-and-drop uploading, uploading via a hidden form, events wired up to
  11. links and dynamically generated content, and ajax requests/file uploads, the whole she-bang. It does all of
  12. that without jQuery. It's [open source](https://github.com/MediaCrush/MediaCrush), if you're looking for a good
  13. example of how all of this can be used in the wild.
  14. Let's walk through some of the things you like jQuery for, and I'll show you how to do it without.
  15. ## Document Querying with CSS Selectors
  16. You like jQuery for selecting content. I don't blame you - it's really cool. Here's some code using jQuery:
  17. ```js
  18. $('div.article p').addClass('test');
  19. ```
  20. Now, here's how you can do it on vanilla JS:
  21. ```js
  22. var items = document.querySelectorAll('div.article p');
  23. for (var i = 0; i < items.length; i++)
  24. items[i].classList.add('test');
  25. ```
  26. Documentation: [querySelectorAll](https://developer.mozilla.org/en-US/docs/Web/API/Document.querySelectorAll), [classList](https://developer.mozilla.org/en-US/docs/Web/API/element.classList)
  27. This is, of course, a little more verbose. However, it's probably a lot simpler than you expected. Works in
  28. IE 8 and newer - except for classList, which works in IE 10 and newer. You can instead use className, which is
  29. a little less flexible, but still pretty easy to work with.
  30. ## Ajax
  31. You want to make requests in JavaScript. This is how you POST with jQuery:
  32. ```js
  33. $.post('/path/to/endpoint', {
  34. parameter: value
  35. otherParameter: otherValue
  36. },
  37. success: function(data) {
  38. alert(data);
  39. });
  40. ```
  41. Here's the same code, without jQuery:
  42. ```js
  43. var xhr = new XMLHttpRequest(); // A little deceptively named
  44. xhr.open('POST', '/path/to/endpoint');
  45. xhr.onload = function() {
  46. alert(this.responseText);
  47. };
  48. var formData = new FormData();
  49. formData.append('parameter', value);
  50. formData.append('otherParameter', value);
  51. xhr.send(formData);
  52. ```
  53. Documentation: [XMLHttpRequest](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest)
  54. Also a bit more verbose than jQuery, but much simpler than you might've expected. Now here's the real kicker:
  55. It works in IE 7, and IE 5 with a little effort. IE actually pioneered XHR.
  56. ## Animations
  57. This is where it starts to get more subjective and breaks backwards compatability. Here's my opinion on the
  58. matter of transitions: dropping legacy browser support for fancy animations is acceptable. I don't think it's
  59. a problem if your website isn't pretty and animated on older browsers. Keep that in mind as we move on.
  60. I want to animate the opacity of a `.foobar` when you hover over it. With jQuery:
  61. ```js
  62. $('.foobar').on('mouseenter', function() {
  63. $(this).animate({
  64. opacity: 0.5
  65. }, 2000);
  66. }).on('mouseleave', function() {
  67. $(this).animate({
  68. opacity: 1
  69. }, 2000);
  70. });
  71. ```
  72. Without jQuery, I wouldn't do this in Javascript. I'd use the magic of CSS animations:
  73. ```css
  74. .foobar {
  75. transition: opacity 2s linear;
  76. }
  77. .foobar:hover {
  78. opacity: 0;
  79. }
  80. ```
  81. <p class="foobar">Hover over this text</p>
  82. <style>.foobar{transition:opacity 2s linear;font-weight:bold;}.foobar:hover{opacity:0.5;}</style>
  83. Documentation: [CSS animations](https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Using_CSS_animations)
  84. Much better, eh? Works in IE 10+. You can do much more complicated animations with CSS, but I can't think of
  85. a good demo, so that's an exercise left to the reader.
  86. ## Tree traversal
  87. jQuery lets you navigate a tree pretty easily. Let's say you want to find the container of a button and remove
  88. all .foobar elements underneath it, upon clicking the button.
  89. ```js
  90. $('#mybutton').click(function() {
  91. $(this).parent().children('.foobar').remove();
  92. });
  93. ```
  94. Nice and succinct. I'm sure you can tell the theme so far - the main advantage of jQuery is a less verbose
  95. syntax. Here's how it's done without jQuery:
  96. ```js
  97. document.getElementById('mybutton').addEventListener('click', function() {
  98. var foobars = this.parentElement.querySelectorAll('.foobar');
  99. for (var i = 0; i < foobars.length; i++)
  100. this.parentElement.removeChild(foobars[i]);
  101. }, false);
  102. ```
  103. A little wordier, but not so bad. Works in IE 9+ (8+ if you don't use addEventListener).
  104. ## In conclusion
  105. jQuery is, of course, based on JavaScript, and as a result, anything jQuery can do can be done without jQuery.
  106. Feel free to [ask me](mailto:sir@cmpwn.com) if you're curious about how I'd do something else without jQuery.
  107. I feel like adding jQuery is one of the first things a web developer does to their shiny new website. It just
  108. isn't really necessary in this day and age. That extra request, 91kb, and load time are probably negligible,
  109. but it's still a little less clean than it could be. There's no need to go back and rid all of your projects of
  110. jQuery, but I'd suggest that for your next one, you try to do without. Keep MDN open in the next tab over and
  111. I'm sure you'll get through it fine.