Skip to content

Instantly share code, notes, and snippets.

@pburtchaell
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pburtchaell/244346f15732237fe7df to your computer and use it in GitHub Desktop.
Save pburtchaell/244346f15732237fe7df to your computer and use it in GitHub Desktop.
Plain JavaScript implementations of common jQuery functions
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.
// Takes two params: element and classname
function hasClass(el, cls) {
return el.className && new RegExp("(\\s|^)" + cls + "(\\s|$)").test(el.className);
}
/**
* 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");
}
// 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