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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:31:49+00:00 2026-05-23T04:31:49+00:00

I am adding an additional className to the body tag, however the className is

  • 0

I am adding an additional className to the body tag, however the className is being added multiple times… some help on how to avoid this would be very welcome!

Thanks

<html>
<head>

<style type="text/css">
.defaultText {
font-size:100%;
}
.myNewClass {
font-size:150%;
color:red;
}
</style>

<script type="text/javascript">
<!--

var state;

window.onload=function() {

obj=document.getElementsByTagName('body')[0];
state=(state==null)?' defaultText':state;
obj.className+=state;

document.getElementById('addClass').onclick=function() {
obj.className+=(obj.className==' myNewClass')?' defaultText':' myNewClass';
state=obj.className;
}
}

-->
</script>

</head>

<body class="thisClassMustStay">

<div>My text here</div><br />
<a href='#' id="addClass">toggle class</a></div>

</body>
</html>
  • 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-23T04:31:50+00:00Added an answer on May 23, 2026 at 4:31 am
    > var state;
    > 
    > window.onload=function() {
    > 
    >   obj = document.getElementsByTagName('body')[0];
    >   state = (state == null)? ' defaultText' : state;
    

    The first time the code is run, state us undefined so it would be better to either set it to null when declaring it or change the above to:

      state = state? ' defaultText' : state;
    

    Given that the first time it is run the expression will evaluate to true, then state will be set to ' defaultText'. Note that there is a space at the start of the string. In some browsers, this space will be removed and in others it will be kept, so much better to remove it.

    >   obj.className += state;
    

    At this point, ‘defaultText’ will be added to the list of classes on the body element. Again there may be issues with leading spaces that are best dealt with by robust hasClass, addClass and removeClass functions (they aren’t particularly difficult to write).

    > 
    >   document.getElementById('addClass').onclick = function() {
    >     obj.className += (obj.className == ' myNewClass')? ' defaultText' :
    >                                                        ' myNewClass';
    

    With functions suggested above, this could be:

      if (hasClassname(obj, 'myNewClass')) {
        addClassname(obj, 'defaultText') ;
      } else {
        addClassname(obj, 'myNewClass') ;
      }
    

    .

    state = obj.className;
    

    }
    }

    If you need help with has/add/removeClassname functions, just ask.

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

Sidebar

Related Questions

When adding an EditItemTemplate of some complexity (mulitple fields in one template), and then
How should I be adding additional search/filter criteria to a Dynamic Data Web Application?
What are my options, if any, of adding additional, arbitrary data to the standard
I'm adding additional languages to a Drupal site that I'm building. Getting the translation
Is selecting by ID-only (or a single identifier) faster than when adding additional identifiers?
I've a jQuery plug-in that operates on some ULs, adding and removing classes etc.
Can anyone help me with this? I have not been able to find anything
Has anyone had any success with adding additional information to a PDF file? We
At work I have been tasked with adding additional functionality to an existing MVC
Just wondering if anyone knows how I would implement additional/custom EndLineCap, StartLineCap & LineJoin

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.