Created
April 10, 2018 00:03
-
-
Save neodigm/539594d86dfe84b11cf3a194a67024ed to your computer and use it in GitHub Desktop.
Easy jQuery migration to vanilla JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// jQuery | |
$(".review").addClass("hide"); | |
// JavaScript | |
[].slice.call( document.querySelectorAll(".review") ).filter( function( _e ){ | |
_e.classList.add("hide"); | |
}); | |
// JavaScript
document.querySelectorAll(".review").forEach(el => el.classList.add("hide"));
// 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
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