diff --git a/snippets/hasClass.md b/snippets/hasClass.md new file mode 100644 index 000000000..5151bab5f --- /dev/null +++ b/snippets/hasClass.md @@ -0,0 +1,13 @@ +### hasClass + +Returns `true` if the element has the specified class, `false` otherwise. + +Use `element.classList.contains()` to check if the element has the specified class. + +```js +const hasClass = (el, className) => el.classList.contains(className); +``` + +```js +hasClass(document.querySelector('p.special')[0],'special') // true +``` diff --git a/snippets/toggleClass.md b/snippets/toggleClass.md new file mode 100644 index 000000000..855ac97b9 --- /dev/null +++ b/snippets/toggleClass.md @@ -0,0 +1,13 @@ +### toggleClass + +Toggle a class for an element. + +Use `element.classList.toggle()` to toggle the specified class for the element. + +```js +const toggleClass = (el, className) => el.classList.toggle(className); +``` + +```js +toggleClass(document.querySelector('p.special')[0],'special') // The paragraph will not have the 'special' class anymore +``` diff --git a/tag_database b/tag_database index f170f51d9..cf5a1a5b9 100644 --- a/tag_database +++ b/tag_database @@ -54,6 +54,7 @@ getType:utility getURLParameters:browser groupBy:array hammingDistance:math +hasClass:browser head:array hexToRGB:utility httpsRedirect:browser @@ -126,6 +127,7 @@ timeTaken:utility toCamelCase:string toDecimalMark:utility toEnglishDate:date +toggleClass:browser toKebabCase:string tomorrow:date toOrdinalSuffix:utility