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 612179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:51:54+00:00 2026-05-13T17:51:54+00:00

I am trying to refine my custom jquery plugin. I have asked this before

  • 0

I am trying to refine my custom jquery plugin. I have asked this before here. The previous problem was resolved. Now I want to make my plugin more flexible. So instead of relying on hardcoded old classes, I am trying to make it rely on the array of values of the select box which dynamically change the classes based on their values. The reason I am doing this, sometimes the class is not there before I click the select option. Sometimes it does for other selector.

To say better what I am trying to achieve:

If the selector has class any of the select box values, remove this class, add new class, based on the latest selected value.

So here is the latest code:

$.fn.classSwitcher = function(options) {
  var baseOptions = {
    classTarget: 'body',
    newClass: ''
    };

  var options = $.extend(baseOptions, options);

  var allVals = [];

  return this.find('option').each(function() {
  //retrieve all select box values
  allVals.push( $(this).val() );

         $(this).click(function() {
           var t = $(this);
           var newClass = (options.newClass) ? options.newClass : t.val();

           // Is it okay to do hasClass to array values?
           if ($(options.classTarget).hasClass(allVals))  {
           $(options.classTarget).removeClass(allVals).addClass(newClass);
           }

           // problem here I guess, can not update the old class with the one
           allVals = newClass;

           $.cookie('cookieNewClass', newClass, { expires: 1 });
           //alert('New Class: ' + $.cookie('cookieNewClass')); // produced expected new class
           //alert(allVals); // produced expected new classes

  });
});
};

The class is added, but like the previous question, I can’t update the targeted class to get replaced with the new one. Please also see the comments in the code for other problem.

Feel free to mark it dup if it is and resolved 🙂

Any hint would be very much appreciated. Thanks

  • 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-05-13T17:51:55+00:00Added an answer on May 13, 2026 at 5:51 pm
    $.fn.classSwitcher = function(options) {
     var baseOptions = {
      classTarget: 'body'
     };
    
      var options = $.extend(baseOptions, options);
    
     var allVals = this.find('option').map(function(){
      return $(this).val();
     });
     var target = $(options.classTarget);
    
     this.change(function(){ 
      var t = $(this);
      var newClass = t.val();
      target.removeClass(allVals.get().join(' ')).addClass(newClass);
     });
    };
    $('select.changer').classSwitcher();
    

    html:

    <select class="changer">
     <option value="apple-80">apple-80</option>
     <option value="apple-100">apple-100</option>
     <option value="apple-120">apple-120</option>
     <option value="apple-300">apple-300</option>
    </select>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to refine my C skills and have been thinking of trying to
I am trying to use this plugin here to create a slider on my
Trying to make this jQuery filter that uses .find case-insensitive. For example, when the
trying to figure out why this is happening - I have an input text
I'm having a bit of a problem trying to refine/refactor a LINQ to Entities
Trying to figure out how I can do this properly. The print_r looks like
Trying to copy the msdn refernce here doesn't work and gives an error I
I feel like this question has probably been asked a thousand times already, so
I have this query. Which seems to be giving me complications. It works if
I'm trying to block all non-localhost attempts to access a Webrick process. This is

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.