{"id":164648,"date":"2017-05-31T13:00:18","date_gmt":"2017-05-31T13:00:18","guid":{"rendered":"https:\/\/premium.wpmudev.org\/blog\/?p=164648"},"modified":"2017-05-29T01:50:02","modified_gmt":"2017-05-29T01:50:02","slug":"using-javascript-instead-of-jquery","status":"publish","type":"post","link":"https:\/\/wpmudev.com\/blog\/using-javascript-instead-of-jquery\/","title":{"rendered":"How to Use JavaScript in WordPress and Lose jQuery Bloat"},"content":{"rendered":"<p>jQuery has become ubiquitous on the web and in WordPress. Many of its components are in the WordPress core software, the admin uses it heavily, and it&#8217;s readily available for front-end use as well. We enqueue it without thought when we need it, but should we always do so?<\/p>\n<p>I tend to think not, and in this article, I&#8217;ll show you why, and how you can achieve the same results without jQuery.<\/p>\n<h2>Why jQuery <em>Is<\/em> Great<\/h2>\n<p><a href=\"https:\/\/wpmudev.com\/blog\/getting-started-jquery\/\" target=\"_blank\">jQuery<\/a> is awesome because it provides an <a href=\"https:\/\/wpmudev.com\/blog\/getting-started-jquery\/\" target=\"_blank\">easy-to-use framework<\/a> that anyone can understand and get started in minutes.<\/p>\n<p>Complex DOM traversals and animations become a single line of strung together functions \u2013 easy-peasy.<\/p>\n<h2>Why jQuery <em>Is Not<\/em> Great<\/h2>\n<p>In many cases, jQuery is nothing more than overhead. You may use 5% of the codebase for your DOM traversal and them a tiny bit more for changing an attribute. The more <a href=\"https:\/\/wpmudev.com\/blog\/javascript-for-wordpress-people\/\" target=\"_blank\">JavaScript<\/a> you need to write the more reason you have to use jQuery. If you&#8217;re only writing a couple of lines, it&#8217;s mostly unused code.<\/p>\n<p>Expanded jQuery clocks in around 250kb and compressed it&#8217;s about 85kb. The version of jQuery core WordPress uses is only about 30kb, but that&#8217;s still way too much if you just need to select a couple of links and change their colors.<\/p>\n<p>To put all that into perspective, if your site has 1 million unique views a month you&#8217;ll generate 30GB of traffic from unused code \u2013 what a waste!<\/p>\n<h2>The Fix: Vanilla JavaScript<\/h2>\n<p>The answer is <a href=\"https:\/\/wpmudev.com\/blog\/javascript-wordpress-basics\/\" target=\"_blank\">plain old vanilla JavaScript<\/a>. jQuery is nothing more than a framework that translates your jQuery code into valid JavaScript. When you write <code>jQuery('#my-link')<\/code> you could have just written <code>document.getElementById('my-link')<\/code>. Yes, it&#8217;s a bit longer, but not 30kb longer!<\/p>\n<p>Let&#8217;s learn how to do a couple of quick common things in JavaScript without jQuery.<\/p>\n<h3>#1. Select Elements<\/h3>\n<p>Selecting elements is almost as easy in vanilla JS as it is in jQuery.<\/p>\n<div class=\"gist\" data-gist=\"ab40698126ea80f103da6f07d4128aa9\" data-gist-file=\"select.js\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/ab40698126ea80f103da6f07d4128aa9.js?file=select.js\">Loading gist ab40698126ea80f103da6f07d4128aa9<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>Where vanilla JS starts to fall short is complex selections like <code>jQuery('span.links ul li:first a.simple')<\/code>. Luckily we usually have control over our class names. I would suggest using unique prefixed classnames, which will allow you to use vanilla JavaScript more easily.<\/p>\n<h3>#2. Traverse The DOM<\/h3>\n<p>Before we start to traverse the DOM, there is one crucial difference in how text works in vanilla JavaScript you need to understand. Consider the following:<\/p>\n<div class=\"gist\" data-gist=\"0f29d75078dc5b5c2e96eb300c9f99ae\" data-gist-file=\"list.html\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/0f29d75078dc5b5c2e96eb300c9f99ae.js?file=list.html\">Loading gist 0f29d75078dc5b5c2e96eb300c9f99ae<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>You&#8217;d expect to be able to select the first list item, it&#8217;s next sibling would be the next list item. In jQuery this is how it works but under the hood text nodes need to be factored in as well. The next sibling of the first list item is actually a text node with the text &#8220;List Item One.&#8221; The next sibling of that node is the second list item.<\/p>\n<p>The two conclusions: text nodes need to be considered when traversing the DOM and text can only be parsed out of text nodes, not their containing elements as you may be used to in jQuery. Here&#8217;s how we could traverse the HTML above:<\/p>\n<div class=\"gist\" data-gist=\"851834545ffda8cf937fb2d54aace599\" data-gist-file=\"listTraversal.js\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/851834545ffda8cf937fb2d54aace599.js?file=listTraversal.js\">Loading gist 851834545ffda8cf937fb2d54aace599<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<h3>#3. Set and Get Attributes<\/h3>\n<p>Getting and setting attributes is pretty easy in vanilla JavaScript. Here&#8217;s how to get and set a custom <code>data-scale<\/code> property:<\/p>\n<div class=\"gist\" data-gist=\"a04697b8822bfe547ff7dfb0c5822a33\" data-gist-file=\"properties.js\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/a04697b8822bfe547ff7dfb0c5822a33.js?file=properties.js\">Loading gist a04697b8822bfe547ff7dfb0c5822a33<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<h3>#4. Looping Through Nodes<\/h3>\n<p>A common trick used when writing JavaScript is to quickly manipulate bunch of nodes together. This requires a loop in vanilla JavaScript while jQuery can take care of this easily (the loop is still there in the background of course).<\/p>\n<div class=\"gist\" data-gist=\"9900a6937713de25e3cc6fdb8c3d84d1\" data-gist-file=\"looping.js\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/9900a6937713de25e3cc6fdb8c3d84d1.js?file=looping.js\">Loading gist 9900a6937713de25e3cc6fdb8c3d84d1<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<h2>Conclusion<\/h2>\n<p>Next time you need to add classes to a plugin-generated menu or create a quick toggle functionality try to give vanilla JavaScript a go instead of whipping out a 35kb library.<\/p>\n<p>While some lines can be a bit more lengthy than the jQuery counterparts, you&#8217;ll still use a <strong>lot<\/strong> less code to get things done. You&#8217;ll save bandwidth, write leaner code and learn some valuable pure JS in the process.<\/p>\n<p>Interested in <a href=\"https:\/\/wpmudev.com\/blog\/javascript-rest-api-jargon\/\" target=\"_blank\">learning JavaScript<\/a> in more depth? Check out our course <a href=\"https:\/\/wpmudev.com\/academy\/courses\/javascript-for-wordpress-developers-2\" target=\"_blank\">JavaScript for WordPress Developers<\/a> in The Academy. If you&#8217;re a member, it&#8217;s free to enroll. And if not, sign up for a free trial and get a month to complete the course!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>jQuery has become ubiquitous on the web and in WordPress. Many of its components are in the WordPress core software, the admin uses it heavily, and it&#8217;s readily available for front-end use as well. We enqueue it without thought when we need it, but should we always do so? I tend to think not, and [&hellip;]<\/p>\n","protected":false},"author":344049,"featured_media":165438,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"blog_reading_time":"","wds_primary_category":0,"wds_primary_tutorials_categories":0,"footnotes":""},"categories":[557],"tags":[9770,505,679],"tutorials_categories":[],"class_list":["post-164648","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development","tag-development-2","tag-javascript","tag-jquery"],"_links":{"self":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/164648","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/users\/344049"}],"replies":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/comments?post=164648"}],"version-history":[{"count":7,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/164648\/revisions"}],"predecessor-version":[{"id":165499,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/164648\/revisions\/165499"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/media\/165438"}],"wp:attachment":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/media?parent=164648"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/categories?post=164648"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/tags?post=164648"},{"taxonomy":"tutorials_categories","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/tutorials_categories?post=164648"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}