Skip to content

Instantly share code, notes, and snippets.

@neodigm
Created April 10, 2018 00:03
Show Gist options
  • Save neodigm/539594d86dfe84b11cf3a194a67024ed to your computer and use it in GitHub Desktop.
Save neodigm/539594d86dfe84b11cf3a194a67024ed to your computer and use it in GitHub Desktop.
Easy jQuery migration to vanilla JavaScript
// jQuery
$(".review").addClass("hide");
// JavaScript
[].slice.call( document.querySelectorAll(".review") ).filter( function( _e ){
_e.classList.add("hide");
});
@neodigm
Copy link
Author

neodigm commented Apr 10, 2018

The one issue that everyone has when migrating from JQuery to JavaScript is a replacement for the simple addClass by Class.
Here is some eloquent and performant code that can save you time and effort because you don’t have to write a loop to iterate a node array.

Like this? Clap this:

https://medium.com/@neo5ive/are-you-mobile-d78cf8a5ec0a

@belozer
Copy link

belozer commented Oct 22, 2018

// JavaScript
document.querySelectorAll(".review").forEach(el => el.classList.add("hide"));

@neodigm
Copy link
Author

neodigm commented Oct 23, 2021

// JavaScript
document.querySelectorAll(".review").forEach(el => el.classList.add("hide"));

This is an ES6 solution, ES5 did not support forEach on a Node List.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment