Home > database >  Run cookies script only if checkbox contains class on page load
Run cookies script only if checkbox contains class on page load

Time:03-02

I have a cookies consent where I have categories like necessary cookies, adveritsing cookies, functional cookies etc.

What do I need to do is to run scripts once checkbox for category has class "jozo" (in this case its checked) on page load. But my script does not work.

On my HTML side its working like this:

<input type="checkbox" id="wt-cli-checkbox-advertisement"  data-id="checkbox-advertisement" checked="checked">

Js side

if ($ ('input#wt-cli-checkbox-advertisement').hasClass('jozo')){
                $(document).ready(function() {
                        Facebook.loadPixel();
                        Analytics.loadPixel();
                        GAds.loadPixel(); 
                        });
                }

I need to also do that if it does not contain class "jozo" then run following script:

 Facebook.disable();
 Analytics.disable();
 GAds.disable();

This needs to be recognised on each page load. Now my scrips are not runing. They only run if I add this but then users can not turn them of in accord of the GDPR law.

$(document).ready(function() {
    Facebook.loadPixel();
    Analytics.loadPixel();
    GAds.loadPixel(); 
    });

Any ideas?

CodePudding user response:

Maybe the checkbox loading after your script running. Convert it to:

$(document).ready(function() {
    if ($('input#wt-cli-checkbox-advertisement').hasClass('jozo')){
       Facebook.loadPixel();
       Analytics.loadPixel();
       GAds.loadPixel(); 
   }
   else {
       Facebook.disable();
       Analytics.disable();
       GAds.disable();
   }
});
  • Related