I started writing JavaScript code because of WordPress back in 2007. JavaScript was not what it is today. It would behave differently on different browsers and there were too many hacks around basic stuff to keep up with all the changes all the time.
So, many of us decided to rely on jQuery — a simple JavaScript library with one single form of syntax that just worked everywhere in all the browsers.
Fast forward to 2019, as a full-time JavaScript Developer Advocate — I advocate modern JavaScript. Because it's super awesome. Though at times I miss the simplicity of jQuery in things like you'd just need a $ sign to pick up an element and do stuff to it.
Now with JavaScript, I find myself doing document.querySelector multiple times in an application. Well, guess what, there's an easy way to just bind that $ sign to your document's document.querySelector.
Here's how you go about it.
const $ = document.querySelector.bind(document);
const $$ = document.querySelectorAll.bind(document);
Now you can use the following:
// Change the background color of a class.
$('.class').style.background="#BADA55";
// Change the inner HTML of an ID.
$('#id').innerHTML="<span>Cool beans!</span>";
// Select all images on the webpage.
$$('img')
// Print the image addresses for all the images on a webpage.
$$('img').forEach(img => console.log(img.src))
Use your code for good and have fun! :)
Peace! ✌️






























This looks cool.
with you example could you still do this though?