Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8648757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:23:17+00:00 2026-06-12T13:23:17+00:00

I am trying to use Cookie so that a default style OR a specific

  • 0

I am trying to use Cookie so that a default style OR a specific style is applied in reference to the anchor tag clicked, even when the browser is closed/reopen.
So if the user clicked the second link, close or refresh the browser and reopen, than the style should still be active, if it is their first time the default should apply.
This is a little over my turf.

Here is the HTML:

<a id="default" href="#/">Default Style</a>
<a id="style2"  href="#/">Style 2</a>
<a id="style3"  href="#/">Style 3</a>

<ol>
<li><span>Hello World</span></li>
</ol>

JQuery: (Compliments of StackOverflow)

<script type="text/javascript">
$('#default').on('click',function(){
    $('ol li span').removeClass(function() {
          return $(this).attr('class');
        }).addClass('default');
});
$('#style2').click(function(){
    $('ol li span').removeClass(function() {
          return $(this).attr('class');
        }).addClass('style2');
});
$('#style3').click(function(){
    $('ol li span').removeClass(function() {
          return $(this).attr('class');
        }).addClass('style3');
});
</script>

CSS:

<style type="text/css">
.default{
color: #333;
}

.style2{
color: #999;
}

.style3{
color: #800;
}
</style>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T13:23:19+00:00Added an answer on June 12, 2026 at 1:23 pm

    There is a cookie plugin for jQuery you can use to set and get cookies, but if browsers older than IE8 are’nt an issue, I’d go for local storage instead and just use the default style for the 1% of visitors using older browsers.

    If you add a class to your links, you can target all of them in one function, and just set the class based on the ID clicked, here’s an example using local storage:

    $(function() {
        if (localStorage.getItem('style')) {
            $('ol li span').addClass(localStorage.getItem('style'));
        }else{
            $('ol li span').addClass('default');
        }
    
        $('.styles').on('click', function(e) {
            e.preventDefault();
            var styles    = ['default', 'style2', 'style3'],
                currStyle = this.id;
    
            $('ol li span').removeClass(styles.join(' ')).addClass(currStyle);
    
            localStorage.setItem('style', currStyle);
        });
    });
    ​
    

    FIDDLE

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use a cookie that is set by other page in
I am trying to save and use a cookie to know what kind of
I have a cookie that I use on my app. It looks like this:
I'm just trying to set and use a cookie but I can't seem to
The site that I'm trying to scrape uses js to create a cookie. What
I'm trying to set a cookie for use across different virtual hosts. Something similar
I am trying to do the following. Use the default model binder to bind
So, I'm trying to use the cookie plugin (seen here: http://grails.org/plugin/cookie ), and overall,
Im trying to use DefaultHttpClient to log into xbox.com. I realize that you cant
I'm using requirejs in a wordpress plugin. I'm trying to use it so that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.