Title: You don't always need jQuery
Slug: you-dont-always-need-jquery
Date: 2014-12-07 18:45:00
Author: Kartones
Lang: en
Tags: Development, Javascript, HTML
Description: How vanilla Javascript can be used for simple tasks instead of jQuery.

<p><em>Disclaimer</em>: Pretty introductory Javascript follows. If you were looking for advanced tips, migration techniques and the like, check directly the links at the end of the article.</p>
<p>It's been at least one year since I last touched client-side javascript. Back then, we were quite pragmatic and relied on jQuery for most Javascript tasks; it's easy, quite well documented and has really good maintenance, bugfixing and updates. But I was also either using it or <a href="http://zeptojs.com/">ZeptoJS</a> even for tiny personal projects, so the other day I thought: "<em>maybe vanilla Javascript and its query selectors are enough for simple tasks</em>". </p>
<p>Chrome and Firefox have always been quite fast adopting changes, but the burden of Internet Explorer was ever-present. Now that everything below IE8 (and soon IE9) is considered not worth the effort and even mobile phones ar on par, I decided to do some small tests. And my results cannot be more satisfying... For trivial stuff you no longer need to rely on jQuery at all.</p>
<p>I'll simply provide four small code examples to summarize the scenarios I had to change: <em>Clicking things, attaching events</em> (with and without delegation/bubbling up), <em>selecting N items and showing/hiding nodes</em>. There are many scenarios where jQuery is of use or maybe not so easily replaceable, but for quick PoCs, pet projects and internal tools... .</p>
<p><u><strong>Click on a node</strong></u></p>
<p><strong>jQuery</strong><br/>
<font face="Courier New">$("myId").click();</font></p>
<p><strong>Vanilla</strong></p>
<p><font face="Courier New">document.getElementById("myId").click();</font></p>
<p><u><strong>Show/Hide</strong></u></p>
<p><strong>jQuery</strong></p>
<p><font face="Courier New">function ToggleSection(eventData) {<br>  $("p").each(function (index) {<br>      $(this).hide();<br>  });<br>    $("s" + eventData.currentTarget.id).show();<br>}</font></p>
<p><strong>Vanilla</strong></p>
<p><font face="Courier New">function ToggleSection(eventData) {<br>  var i, len, $p = document.querySelectorAll("p");<br>  for (i = 0, len = $p.length; i &lt; len; i++) {<br>    $p[i].style.display = 'none';<br>  }<br>  document.getElementById("s" + eventData.target.id).style.display = 'block';<br>}</font></p>
<p><strong></strong> </p>
<p><u><strong>Attach event handlers</strong></u></p>
<p><strong>jQuery</strong></p>
<p><font face="Courier New">$("h3").each.on("click", ToggleSelection);</font></p>
<p><strong>Vanilla</strong></p>
<p><font face="Courier New">var i, len, $h3 = document.querySelectorAll("h3");<br>for (i = 0, len = $h3.length; i &lt; len; i++) {<br>  <font color="#009900">// Option A: Overriding previous handlers</font><br>  // $h3[i].onclick = ToggleSection;<br>  <font color="#009900">// Option B: Adding a handler keeping existing ones</font><br>  </font><span style="font-family: 'Courier New';">$h3[i].addEventListener("click", ToggleSection);</span><font face="Courier New"><br>}</font></p>
<p><u><strong>Attach event handlers (event delegation)</strong></u></p>
<p><strong>jQuery</strong></p>
<p><font face="Courier New">$("#bookmarks").on("click", "h3", ToggleSelection);</font></p>
<p><strong>Vanilla</strong></p>
<p><font face="Courier New">document.getElementById("bookmarks").onclick = function(eventData) {<br>  var target;<br>  eventData = eventData || event;<br>  target = eventData.target || eventData.srcElement;<br>  if (target.nodeName == 'H3') {<br>    ToggleSection(eventData);<br>    return false;<br>  } else {<br>    return;<br>  }<br>};</font></p>
<p>I have omitted other query selectors and additional properties but Mozilla Developer Network has <a href="https://developer.mozilla.org/en-US/docs/Web/API/Document.querySelectorAll">really good documentation for them</a>.</p>

<p>
A few related articles:
<ul>
    <li><a href="http://blog.garstasio.com/you-dont-need-jquery/">Post series</a> with very detailed guides of migrating out from jQuery</li>
    <li><a href="https://www.sitepoint.com/dom-manipulation-vanilla-javascript-no-jquery/">A Sitepoint article</a> that is a great source of DOM manipulation basics with vanilla JS (2017)</li>
    <li><a href="https://css-tricks.com/now-ever-might-not-need-jquery/">(Now More Than Ever) You Might Not Need jQuery</a> (2017)</li>
    <li>Another big list of equivalents in vanilla JS: <a href="https://github.com/nefe/You-Dont-Need-jQuery#readme">You Don't Need jQuery</a> (2019)</li>
    <li><a href="https://tobiasahlin.com/blog/move-from-jquery-to-vanilla-javascript/">Cheat-sheet for moving from jQuery to vanilla JavaScript</a> (2019)</li>
</ul>
</p>

<p><br/></p>
<p>Update #1: Added </span><a href="https://developer.mozilla.org/en-US/docs/Web/API/EventTarget.addEventListener">.addEventListener</a> alternative to .onclick to keep existing handlers. Thanks <a href="https://twitter.com/JohnHackworth">@JohnHackworth</a>.</p>
<p>Update #2: Added an excellent and way more complete reference at the end.</p>
<p>Update #3: Added another nice article link.</p>
<p>Update #4: Added yet another in-depth article link.</p>
<p>Update #5: Added greatly detailed resource link.</p>
<p>Update #6: Added a cheat-sheet.</p>
