Instructions

Close an element and hide it across the whole site

If you would like to hide the elements across the whole site, paste the code below into the project settings. NOT in the page settings

Copy the code below and paste it into the <head> tag.

<script defer src="https://cdnjs.cloudflare.com/ajax/libs/js-cookie/2.2.0/js.cookie.min.js"></script>
Copy Code

<!-- Cookies Script -->
<script defer src="https://cdnjs.cloudflare.com/ajax/libs/js-cookie/2.2.0/js.cookie.min.js"></script>

Copy the code below and paste it into the before </body> tag.

<script>

document.addEventListener('DOMContentLoaded', () => {
	const CLOSED_COOKIE = 'removeElements';
	
  // Targets your specific elements
	const removedElements = document.querySelectorAll('.your-banner-class'); // Replace this class with the class of the element you would like to hide. If you want to hide multiple elements, give them all this class.
	const closeButton = document.querySelector('.your-close-button-class'); // Replace this class with the class of your close button.

	// See if cookie exists on load and hide the elements if it does; and stops the rest of the function from running.
	const isClosed = Cookies.get(CLOSED_COOKIE);
	if (isClosed) {
		removeElements();

		return;
	}

	// When the user clicks on the closeButton this functions runs which hides the element and saves the users input in a cookie
	closeButton.addEventListener('click', () => {
		setCookie()
		removeElements()
	})

	// Removes all the targeted elements from the DOM.
	function removeElements() {
		removedElements.forEach((element) => {
			element.remove();
		})
	}

	// Stores the users input in a cookie.
	function setCookie() {
		Cookies.set(CLOSED_COOKIE, 'removeElements', { expires: 1 }); // expires: is how many days the cookies will be saved. Default is 1 day (24 hours).
	}
});

</script>
Copy Code

<script>document.addEventListener('DOMContentLoaded', () => {
const CLOSED_COOKIE = 'removeElements';

 // Targets your specific elements
const removedElements = document.querySelectorAll('.your-banner-class'); // Replace this class with the class of the element you would like to hide. If you want to hide multiple elements, give them all this class.
const closeButton = document.querySelector('.your-close-button-class'); // Replace this class with the class of your close button. // See if cookie exists on load and hide the elements if it does; and stops the rest of the function from running.
const isClosed = Cookies.get(CLOSED_COOKIE);
if (isClosed) {
removeElements(); return;
} // When the user clicks on the closeButton this functions runs which hides the element and saves the users input in a cookie
closeButton.addEventListener('click', () => {
setCookie()
removeElements()
}) // Removes all the targeted elements from the DOM.
function removeElements() {
  removedElements.forEach((element) => {
    element.remove();
   })
} // Stores the users input in a cookie.
function setCookie() {
Cookies.set(CLOSED_COOKIE, 'removeElements', { expires: 1 }); // expires: is how many days the cookies will be saved. Default is 1 day (24 hours).
}
});</script>