Hide Element After Click w/Cookie
Hide an element on your Webflow site after click with a cookie via this Javascript snippet. This is perfect for welcome modals, cookie notifications, notification alerts and other items that only need to be displayed a single time.
Hide an element on your Webflow site after click with a cookie via this Javascript snippet. This is perfect for welcome modals, cookie notifications, notification alerts and other items that only need to be displayed a single time.
How to add it
- Add the Javascript code to the closing body of your pages custom code
- Update the class name with your specified class (default cookie-hide-element
- Add cookie-hide-trigger class to the button that closes the modal/window
- If you want to dynamically set the cookie expire date then you can do so via the cookie-expire-number class, otherwise you can update with your own expiration date
The code
Javascript
$.getScript('https://cdnjs.cloudflare.com/ajax/libs/js-cookie/2.2.1/js.cookie.js', function() {
var cookieName = 'notificationClosed';
var cookieValue = 'userClosedIt';
var cookieExpire = Number($('.cookie-expire-number').text());
if(typeof Cookies.get(cookieName) !== 'undefined') $('.cookie-hide-element').remove();
$('.cookie-hide-trigger').click(function() {
Cookies.set(cookieName, cookieValue, { expires: cookieExpire });
});
});