Added hasClass/toggleClass snippets
This commit is contained in:
13
snippets/hasClass.md
Normal file
13
snippets/hasClass.md
Normal file
@ -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
|
||||||
|
```
|
||||||
13
snippets/toggleClass.md
Normal file
13
snippets/toggleClass.md
Normal file
@ -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
|
||||||
|
```
|
||||||
@ -54,6 +54,7 @@ getType:utility
|
|||||||
getURLParameters:browser
|
getURLParameters:browser
|
||||||
groupBy:array
|
groupBy:array
|
||||||
hammingDistance:math
|
hammingDistance:math
|
||||||
|
hasClass:browser
|
||||||
head:array
|
head:array
|
||||||
hexToRGB:utility
|
hexToRGB:utility
|
||||||
httpsRedirect:browser
|
httpsRedirect:browser
|
||||||
@ -126,6 +127,7 @@ timeTaken:utility
|
|||||||
toCamelCase:string
|
toCamelCase:string
|
||||||
toDecimalMark:utility
|
toDecimalMark:utility
|
||||||
toEnglishDate:date
|
toEnglishDate:date
|
||||||
|
toggleClass:browser
|
||||||
toKebabCase:string
|
toKebabCase:string
|
||||||
tomorrow:date
|
tomorrow:date
|
||||||
toOrdinalSuffix:utility
|
toOrdinalSuffix:utility
|
||||||
|
|||||||
Reference in New Issue
Block a user