My personal website https://leviolson.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

84 lines
4.5 KiB

<h1>Vanilla JS Basics</h1>
<p><em>Warning: This article turned out to be a bit of rant. So if you are the type to get offended when I say negative things about JavaScript, you are welcome to leave.</em></p>
<p>JavaScript is what I consider to be an &quot;easy&quot; language. Now what do I mean by that? Well, its usually the first <em>scripting</em> language that new developers learn, and you can get started with very little &quot;training&quot;. It has a relatively simple syntax. It can be very powerful. So from my point of view, its an easy language to pick up. And therein lies the problem.</p>
<p>So many of the JavaScript programmers out there today hear the term &quot;JavaScript&quot; and think in there mind &quot;jQuery&quot;. Now that&#39;s great to have that kind of brand recognition if you are jQuery, but if you are looking to be an efficient programmer that doesn&#39;t put holes in everything you make, you may want to consider that JavaScript is NOT jQuery.</p>
<p>Additionally, we need to start thinking about the consequences of using third-party libraries for everything. Longer load times, additional security risks, more memory consumption, and the list goes on.</p>
<p>I want to give you at least a few little snippets of vanilla JavaScript that will get you on your way to not importing jQuery on every project as a <em>step 1</em>. It should be near the end of your steps and only as needed.</p>
<p><em>Disclaimer: Vanilla JavaScript will be longer (i.e. more verbose), but this isn&#39;t always a bad thing. Keep in mind that we are eliminating the import of another package, and all the bloat that comes with it.</em></p>
<h2>Assumptions</h2>
<p>For starters, I&#39;m assuming that you getting this far means that you at least have heard of JavaScript. If you have not heard of jQuery, <strong>leave now</strong>, I don&#39;t want &quot;introducing someone to jQuery&quot; to be what you get out of this.</p>
<p>Let&#39;s begin...</p>
<h3>Selecting Element in the DOM</h3>
<p>jQuery</p>
<pre><code> $(&#39;#myID&#39;)</code></pre>
<p>Vanilla JavaScript</p>
<pre><code> document.getElementById(&#39;myID&#39;)</code></pre>
<p>Again, note that vanilla JavaScript is longer, but its actually more descriptive of what is going on here. Unless you are familiar with jQuery (yes, I know you are) the jQuery syntax doesn&#39;t tell you what its doing.</p>
<h3>Replace Text/HTML in the DOM</h3>
<p>jQuery</p>
<pre><code> $(&#39;#myID&#39;).html(&#39;here is the replacement&#39;)</code></pre>
<p>Vanilla JavaScript</p>
<pre><code> document.getElementById(&#39;myID&#39;).innerHTML(&#39;here is the replacement&#39;)</code></pre>
<p>Very similar eh. However, there is actually some performance gain by using vanilla JavaScript. See <a href="https://stackoverflow.com/questions/3563107/jquery-html-vs-innerhtml#answer-3563136">here</a>.</p>
<h3>Click Handling in the DOM</h3>
<p>jQuery</p>
<pre><code> $(&#39;#myID&#39;).click(function() {
alert(&#39;Clicked it!&#39;)
})</code></pre>
<p>Vanilla JavaScript</p>
<pre><code> document.getElementById(&#39;myID&#39;).addEventListener(&#39;click&#39;, function(e) {
alert(&#39;Clicked it!&#39;)
})</code></pre>
<p>So easy.</p>
<h3>Advanced Queries and Iteration</h3>
<p>jQuery</p>
<pre><code> $(&#39;.item&#39;).hide()</code></pre>
<p>Vanilla JavaScript</p>
<pre><code> var items = document.getElementsByClassName(&#39;.items&#39;)
for (var i = 0; i &lt; items.length; i++) {
item[i].style.display = &#39;none&#39;
}</code></pre>
<p>We&#39;re starting to see the verbosity I mentioned above, but again, remember that we aren&#39;t loading in a massive third-party library!</p>
<h3>Even More Advanced</h3>
<p>See Patrick Kunka&#39;s article <a href="https://blog.wearecolony.com/a-year-without-jquery/">A Year Without jQuery</a></p>
<p>Patrick and I agree on many of the same points and his article articulates some helper functions that can be used to perform more advanced loops, child selection and child indexing. I highly recommend you read through his article.</p>
<h2>Conclusion</h2>
<p>If you find that your requirements heavily rely on JavaScript for DOM manipulation, or that you need animations such as the ones provided by jQuery, then don&#39;t let me stop you. But if you only need some of what was covered above, or you want to put a priority on performance, then you should really consider going with plain vanilla JavaScript and leave the dependencies behind. You won&#39;t regret it.</p>