Last active
August 29, 2015 14:03
-
-
Save pburtchaell/244346f15732237fe7df to your computer and use it in GitHub Desktop.
Plain JavaScript implementations of common jQuery functions
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
List of the browsers which support the features: | |
- IE9+ | |
- Firefox 3.5+ | |
- Opera 9+ | |
- Safari 4+ | |
- Chrome 1+ | |
- iPhone and iPad iOS1+ | |
- Android phone and tablets 2.1+ | |
- Blackberry OS6+ | |
- Windows 7.5+ | |
- Mobile Firefox | |
- Opera Mobile | |
If you only need to support more modern browsers like IE10+, Chrome, Firefox, Opera and Safari, you could also start using the HTML5 classList functionality, which makes adding and removing classes easy. |
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
// Takes two params: element and classname | |
function hasClass(el, cls) { | |
return el.className && new RegExp("(\\s|^)" + cls + "(\\s|$)").test(el.className); | |
} | |
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
/** | |
* If you only need to support more modern browsers like IE10+, | |
* Chrome, Firefox, Opera and Safari, you could also start using | |
* HTML5’s classList functionality which makes adding and removing | |
* classes even easier. | |
*/ | |
if ("classList" in document.documentElement) { | |
// Add class | |
element.classList.add("bar"); | |
// Remove class | |
element.classList.remove("foo"); | |
// Check if element has class | |
element.classList.contains("foo"); | |
// Toggle class | |
element.classList.toggle("active"); | |
} |
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
// Takes two params: element and classname | |
function removeClass(el, cls) { | |
var reg = new RegExp("(\\s|^)" + cls + "(\\s|$)"); | |
el.className = el.className.replace(reg, " ").replace(/(^\s*)|(\s*$)/g,""); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment