0

I have some links and calls the click function in js when click on specific link. like below

   jQuery(document).ready(function() { 
       jQuery("a.music").on( 'click', function () {
       var tags = jQuery(this).attr("data-tag");
       document.cookie = "tags=" + tags; 
       console.log(document.cookie);
       location.reload();
       jQuery(this).find('h4').addClass("a-active");
       jQuery(this).find('h4').css("color","red");
         });
    });

The thing is I have to set cookie and get this cookie on that current page so I have to reload the page to get the update cookie everthing times click another link. but after the page reloaded, all the css or adding new class that I did by jquery all dissapear. Like it just happens for a while untils the page reload. Please give me ideal if you know what I did wrong here. Thank you

1 Answer 1

0

I understand, that you want "a.music" remain set active after you reload the page.

For that you can check whether the cookie is set or not if it is set you make active to the "a.music" else it will remain as same.

var tags = getCookie('tags');
jQuery('a.music').each(function(){
    var dTag = jQuery(this).attr("data-tag");
    if(dTag == tags)
    {
        jQuery(this).find('h4').addClass("a-active");
        jQuery(this).find('h4').css("color","red");
    }
});
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.