diff --git a/README.md b/README.md index 46f01c489..c708e58a3 100644 --- a/README.md +++ b/README.md @@ -91,9 +91,11 @@ * [`elementIsVisibleInViewport`](#elementisvisibleinviewport) * [`getScrollPosition`](#getscrollposition) * [`getURLParameters`](#geturlparameters) +* [`hasClass`](#hasclass) * [`httpsRedirect`](#httpsredirect) * [`redirect`](#redirect) * [`scrollToTop`](#scrolltotop) +* [`toggleClass`](#toggleclass) @@ -1762,6 +1764,29 @@ getURLParameters('http://url.com/page?name=Adam&surname=Smith'); // {name: 'Adam [⬆ Back to top](#table-of-contents) +### 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); +``` + +
+Examples + +```js +hasClass(document.querySelector('p.special'), 'special'); // true +``` + +
+ + +[⬆ Back to top](#table-of-contents) + + ### httpsRedirect Redirects the page to HTTPS if its currently in HTTP. Also, pressing the back button doesn't take it back to the HTTP page as its replaced in the history. @@ -1835,6 +1860,29 @@ scrollToTop(); +[⬆ Back to top](#table-of-contents) + + +### 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); +``` + +
+Examples + +```js +toggleClass(document.querySelector('p.special'), 'special'); // The paragraph will not have the 'special' class anymore +``` + +
+ + [⬆ Back to top](#table-of-contents) ## Date diff --git a/docs/index.html b/docs/index.html index f49e7dd18..712fa0b44 100644 --- a/docs/index.html +++ b/docs/index.html @@ -160,9 +160,11 @@ elementIsVisibleInViewport getScrollPosition getURLParameters +hasClass httpsRedirect redirect scrollToTop +toggleClass

Date

getDaysDiffBetweenDates @@ -859,6 +861,13 @@ Pass location.search as the argument to apply to the current
getURLParameters('http://url.com/page?name=Adam&surname=Smith'); // {name: 'Adam', surname: 'Smith'}
 
+

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.

+
const hasClass = (el, className) => el.classList.contains(className);
+
+
hasClass(document.querySelector('p.special'), 'special'); // true
+

httpsRedirect

Redirects the page to HTTPS if its currently in HTTP. Also, pressing the back button doesn't take it back to the HTTP page as its replaced in the history.

Use location.protocol to get the protocol currently being used. If it's not HTTPS, use location.replace() to replace the existing page with the HTTPS version of the page. Use location.href to get the full address, split it with String.split() and remove the protocol part of the URL.

@@ -889,6 +898,13 @@ Scroll by a fraction of the distance from the top. Use window.requestAnima
scrollToTop();
 
+

toggleClass

+

Toggle a class for an element.

+

Use element.classList.toggle() to toggle the specified class for the element.

+
const toggleClass = (el, className) => el.classList.toggle(className);
+
+
toggleClass(document.querySelector('p.special'), 'special'); // The paragraph will not have the 'special' class anymore
+

Date

getDaysDiffBetweenDates

Returns the difference (in days) between two dates.

diff --git a/snippets/hasClass.md b/snippets/hasClass.md index 3a332d169..eb3fc086d 100644 --- a/snippets/hasClass.md +++ b/snippets/hasClass.md @@ -9,5 +9,5 @@ const hasClass = (el, className) => el.classList.contains(className); ``` ```js -hasClass(document.querySelector('p.special'),'special') // true +hasClass(document.querySelector('p.special'), 'special'); // true ``` diff --git a/snippets/toggleClass.md b/snippets/toggleClass.md index fb773f261..eb9f17504 100644 --- a/snippets/toggleClass.md +++ b/snippets/toggleClass.md @@ -9,5 +9,5 @@ const toggleClass = (el, className) => el.classList.toggle(className); ``` ```js -toggleClass(document.querySelector('p.special'),'special') // The paragraph will not have the 'special' class anymore +toggleClass(document.querySelector('p.special'), 'special'); // The paragraph will not have the 'special' class anymore ```