Code

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 Element After Click w/Cookie preview

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

  1. Add the Javascript code to the closing body of your pages custom code
  2. Update the class name with your specified class (default cookie-hide-element
  3. Add cookie-hide-trigger class to the button that closes the modal/window
  4. 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 });
  });
});


                    

Browse similar code snippets

Language

More from Corey Moen

Profile →

Related Webflow questions

All answers →

Similar code snippets

All code snippets →