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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:51:48+00:00 2026-06-13T00:51:48+00:00

I am trying to implement a script to set different class name on a

  • 0

I am trying to implement a script to set different class name on a specific element…

Let’s suppose the dom looks like this:

<body class='pre-existing-class-name'>

If I make

smartToogle('body', 'new-class');
// the dom should look like this
// <body class='pre-existing-class-name new-class'>
smartToogle('body', 'new-class-2');
// the dom should look like this
// <body class='pre-existing-class-name new-class-2'>

I did the following code but it does not work:

var smartToogle = function (element, newClassName) {
    var oldClassName;
    var $element = $(element);

    $element.addClass(newClassName);
    if (oldClassName !== newClassName) {
        $element.removeClass(oldClassName);
    }
    oldClassName = newClassName;
};

Requirements:
1) I am using query
2) I would like to pass just one class name, the new one.


Solution:
The following code works but I do not like it because it uses global variable.
Any hint to fix it?

function myToggle(newClassName) {
    if (window.oldClassName) {
         $('body').toggleClass(window.oldClassName);
    }
    window.oldClassName = newClassName;
    $('body').toggleClass(newClassName);
}
  • 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-13T00:51:49+00:00Added an answer on June 13, 2026 at 12:51 am

    You can use data attribute for the element, that is accessible using

    $(element).data(attrib_name)
    

    Just a small change is required in your method

    function myToggle(newClassName) {
        if (window.oldClassName) {
             $('body').toggleClass(window.oldClassName);
        }
        window.oldClassName = newClassName;
        $('body').toggleClass(newClassName);
    }
    

    can be replaced with

    function myToggle(element, newClassName) {
        if ($(element).data('oldClassName')) {
             $(element).toggleClass($(element).data('oldClassName'));
        }
        $(element).data('oldClassName', newClassName)
        $(element).toggleClass(newClassName);
    }
    

    Hope this solves it for you.

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

Sidebar

Related Questions

I've been trying tom implement the following script which looks like a great date
I'm trying to implement Orbit Slider in my homepage using this code: <script type=text/javascript
I've found this login script and I'm trying to implement it into my website
My PHP knowledge is limited and I am trying to implement http://www.tutorialchip.com/php-download-file-script/ this script
I was trying to implement multithreaded script execution in Unity3d, but it seems that
I'm trying to implement an script that reads the content(files and folders) of a
I've been trying to implement a bash script that reads from wordnet's online database
Trying to implement a search similar to here .This searches properties based on city,locality,property
I am trying to implement simple xhr abstraction, and am getting this warning when
I'm trying to implement the vim script from the book Learning vi and vim

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.