Last active
April 17, 2018 17:40
-
-
Save atsea/6fd9a5f29215f7393894b89c06d76023 to your computer and use it in GitHub Desktop.
jQuery to JS: removeClass, addClass, hasClass translated
This file contains 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 hasClass() | |
$('sel').hasClass('classname'); | |
/** | |
* Translating className properties from jQuery to plain JS v1.0 | |
* Created by Christopher Leonard (https://www.atseadesign.com) | |
* Copyright 2018 Christopher Leonard | |
* Licensed under GNU (https://opensource.org/licenses/GPL-3.0) | |
* | |
* Browser support: <https://caniuse.com/#search=classList> | |
* | |
* For IE 11 support use the classList polyfill (https://github.com/eligrey/classList.js) | |
*/ | |
// JS classList contains() | |
el.classList.contains('classname'); | |
// jQuery addClass() | |
$('sel').addClass('classname'); | |
// JS classList add() | |
el.classList.add('classname'); | |
// jQuery removeClass() | |
$('sel').removeClass('classname'); | |
// JS classList remove() | |
el.classList.remove('classname'); | |
// jQuery toggleClass() | |
$('sel').toggleClass('classname'); | |
// JS classList toggle() | |
el.classList.toggle('classname'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment