Search code examples
javascriptjquerycookiessetcookie

Remove a div after is clicked


I want to remove the parent of a div after it is clicked and store a cookie "clicked" with value true. I use JavaScript Cookie to store cookie. So I have a blog with adsense and I want to hide my ads to protect from invalid activity after user click on it 1 time.

This is my code:

  $("#firstCode").click(function () {
       $(this).parent().hide();

       var date = new Date();
       var h = "1";
       date.setTime(date.getTime() + (h * 60 * 60 * 1000));

       Cookies.set('clicked', 'true', {expires: date, path: '/'});
  });

and html code:

<div id="codeWrapper">
   <div id="firstCode">
      <script type="text/javascript">
         google_ad_client = "ca-pub-7094677798399606";
         google_ad_slot = "8373705259";
         google_ad_width = 728;
         google_ad_height = 90;
      </script>
      <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
      </script>
</div>

but it doesn't work. If someone can find where is my problem, I very appreciate it.


Solution

  • You can use jquery remove to remove an element from DOM

    $("#firstCode").click(function () {
      var date = new Date();
      var h = "<?php echo html_entity_decode(get_option('fs_time_cookie')); ?>";
           date.setTime(date.getTime() + (h * 60 * 60 * 1000));
           Cookies.set('clicked', 'true', {expires: date, path: '/'});
       $(this).parent().remove();
      });