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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:51:14+00:00 2026-06-10T08:51:14+00:00

I would like the ability to add a class to an element and not

  • 0

I would like the ability to add a class to an element and not replace what classes are already there.

Here is my javascript so far:

function black(body) {
var item = document.getElementById(body);
    if (item) {
        item.className=(item.className=='normal')?'black':'normal';
    }
}

That piece of javascript replaces the existing classes with black. If the class already is black then it is changed to normal.

I would like to somehow combine the script above with the script below script, which adds a class, instead of replacing all existing classes.

var item = document.getElementById("body");
item.className = item.className + " additionalclass";
  • 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-10T08:51:16+00:00Added an answer on June 10, 2026 at 8:51 am

    Here are several plain javascript functions you can use for manipulating class names in plain javascript. It takes a little extra work in these functions to match whole class names only and avoid any extra spaces before/after classnames:

    function removeClass(elem, cls) {
        var str = " " + elem.className + " ";
        elem.className = str.replace(" " + cls + " ", " ").replace(/^\s+|\s+$/g, "");
    }
    
    function addClass(elem, cls) {
        elem.className += (" " + cls);
    }
    
    function hasClass(elem, cls) {
        var str = " " + elem.className + " ";
        var testCls = " " + cls + " ";
        return(str.indexOf(testCls) != -1) ;
    }
    
    function toggleClass(elem, cls) {
        if (hasClass(elem, cls)) {
            removeClass(elem, cls);
        } else {
            addClass(elem, cls);
        }
    }
    
    function toggleBetweenClasses(elem, cls1, cls2) {
        if (hasClass(elem, cls1)) {
            removeClass(elem, cls1);
            addClass(elem, cls2);
        } else if (hasClass(elem, cls2)) {
            removeClass(elem, cls2);
            addClass(elem, cls1);
        }
    }
    

    If you wanted to toggle between the black and normal classes without affecting any other classes on the specified object, you could do this:

    function black(id) {
        var obj = document.getElementById(id);
        if (obj) {
            toggleBetweenClasses(obj, "black", "normal");
        }
    }
    

    Working example here: http://jsfiddle.net/jfriend00/eR85c/

    If you wanted to add the “black” class unless “normal” was already present, you could do this:

    function black(id) {
        var obj = document.getElementById(id);
        if (obj && !hasClass(obj, "normal")) {
            addClass(obj, "black");
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a logging class that, well, logs things. I would like to add
I would like to give the user the ability to insert custom elements in
I would like to find a replacement for list.com, specifically the ability to accept
What I would like to accomplish from my Android application is an ability to
I have a sample dataform and I would like to remove the ability to
In php, I would like to have the ability to re-order an associative array
Is Delphi have any ability to write script of IDE actions? I would like
I have 2 classes and in the Persons class I want to add the
I would like the ability to have a test ClickOnce server for my applications
I would like to create a ListView in Android where I have the ability

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.