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

  • SEARCH
  • Home
  • 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 8198071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:52:25+00:00 2026-06-07T05:52:25+00:00

First sorry im a bit of a beginner in jquery. My problem is that

  • 0

First sorry im a bit of a beginner in jquery.

My problem is that im trying to make a class switcher with jquery cookie.

It works fine only one problem.
When i select the jobs on the main page every job has an unique id, and has a button next to it, when the user clicks on it it saves it to the favorites clicks again removes it.
If its saved the button cganges to yellow, if removed changes to blue.

I was able to keep the class after page refresh, but my problem is what i really cant solve is, if i have more jobs listed click on it keeps the class, and i add a nother it removes the class from the other and adds it whats clicked, so it not keeping it on multiple only one.

Could please someone give me a hint?

My code:

html

<a href="#" id="<?php echo $res->info_id; ?>" class="favorite btn btn-primary btn-mini" title="Add to favorite">
  <i class="icon-star icon-white"></i>
</a>

jQuery

(function () {

//save to favorites
$('.favorite').on('click', function(e){
    e.preventDefault();
    var data = [],
    newclass = 'btn-warning',
    oldcalss = 'btn-primary',
    fav = $(this);
    favId = fav.attr('id'),

    $.ajax({
      type: "POST",
      url: base_url + 'ajax/add_favorite/' + favId,
      data: favId,
      dataType: "json",
      success: function(data) { 
        if(data.status == "removed"){
          $.cookie('keepClass', 'btn-primary');
          $.cookie('jId', favId);
          fav.removeClass(newclass)
          .addClass($.cookie('keepClass'))
          .attr("title", "Add to favorites");
        } else {
          $.cookie('keepClass', 'btn-warning');
          $.cookie('jId', favId);
          fav.removeClass(oldcalss)
          .addClass($.cookie('keepClass'))
          .attr("title", "Remove from favorites");;
        }
      }
    });
});

$('#' + $.cookie('jId')).attr('class', "btn btn-mini " + $.cookie('keepClass'));

})(jQuery);

thank you

  • 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-07T05:52:27+00:00Added an answer on June 7, 2026 at 5:52 am

    First thank you all for the replyes, i gave all of you an upvote, but i sat down today and managed to do it with my logic.

    var cookieName = 'fav_';
    $('.favorite').each(function(){
        var id = $(this).attr('id'), cookie = cookieName + id;
        if($.cookie(cookie) !== 'save' ){
            $(this).addClass('btn-primary');
        } else if($.cookie(cookie) !== 'remove') {
            $(this).addClass('btn-warning');
        }
    
    }).on('click', function(e){
        e.preventDefault();
        var fav = $(this);
        var id = fav.attr('id');
    
        $.ajax({
            type: "POST",
            url: base_url + 'ajax/add_favorite/' + id,
            data: id,
            dataType: "json",
            success: function(data)
            {   
                if(data.status == "removed")
                {
                    $.cookie(cookieName + $(fav).attr('id'), 'remove');
                    $(fav).removeClass('btn-warning').addClass('btn-primary');
                } else {
                    $.cookie(cookieName + $(fav).attr('id'), 'save');
                    $(fav).removeClass('btn-primary').addClass('btn-warning');
                }
            }
    
        });
    
    });
    

    thank you for all again

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

Sidebar

Related Questions

I'm sorry to make my first question here a bit of a simple one
first sorry if its a stupid question but im lost a bit. So when
First sorry for my english. I am currently trying to develop a JAVA webservice
First sorry I'm a big beginner. Would like to ask a more experienced developers
first of all, sorry if the title is a bit confusing. i'm planning to
I am a bit rusty with mysql and trying to jump in again..So sorry
Hello first sorry if its a stupid question, but I amm a beginner, and
Sorry for a little bit beginner question. There are vector and vector of pairs
Sorry, I know that this topic has been covered a bit. I've read the
I'm running into a bit of a problem on a site I'm trying to

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.