5/3/2018 - 10:56 AM

es6 selectors as in jquery

es6 selectors as in jquery

In jQuery, the $ selector is basically a function, so you can easily turn that into a function in vanilla JS like so:

const $ = (x) => document.querySelector(x); // Thus, $('.class') or $('#id'). Done and done!

const $$ = (x) => document.querySelectorAll(x); // To select multiple elements via a nodelist. Ex: $$('.multiple-elements')

And if you'd prefer to use getElementById:

const $id = (x) => document.getElementById(x); // Ex: $('id')