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

The Archive Base Latest Questions

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

Given a webpage that has a div such as: <div id=test class=test>something</div> Is it

  • 0

Given a webpage that has a div such as:

<div id="test" class="test">something</div>

Is it possible to insert additional class names into the list, so that it read something like:

<div id="test" class="test test1 test2">something</div>

? I could easily change the text between the divs using innerHTML, but can’t figure out a way to add to the class list.

  • 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-25T11:31:49+00:00Added an answer on May 25, 2026 at 11:31 am

    What you want to do is invalid (An element cannot have more than on ID). An ID can’t contain spaces either, however Chrome seems to allow it. I’m not sure about other browsers, but in Chrome you could do something like this to update it:

    document.getElementById('test').id += ' test1 test2';
    

    But then to access that element you’ll have to use:

    document.getElementById('test test1 test2');
    

    Keep in mind that this is invalid so there’s no reason that it should continue to work in future versions of a browser just because it works now.

    Edit

    You could use these Javascript functions I wrote the other day for accessing adding and removing classes. The addClass function will accept an array, or array-like object and add the class to each element. If it’s not array-like it will just add the class to that element. Use it like:

    addClass(element, 'test1');
    addClass(element, 'test2');
    

    Code:

    function hasClass(el,c){
        var elc = ' '+el.className+' ';
        if(elc.indexOf(' '+c+' ') < 0)
            return false;
        return true;    
    }
    
    function addClass(els, c){
        if(typeof els.length === 'number' && typeof els[els.length-1] !== 'undefined')
            for(var i = els.length; i--;)
                _addClass(els[i], c);
        else
            _addClass(els, c);
    
        function _addClass(el, c){
            if(c.indexOf(' ') >= 0)
                return false;
            if(hasClass(el, c))
                return true;
            el.className += ' '+c;
            return true;
        }
    }
    
    function removeClass(els, c){
        if(typeof els.length === 'number' && typeof els[els.length-1] !== 'undefined')
            for(var i = els.length; i--;)
                _removeClass(els[i], c);
        else
            _removeClass(els, c);
    
        function _removeClass(el, c){
            if(c.indexOf(' ') > -1)
                return false;
            if(!hasClass(el, c))
                return true;
            var elc = (' '+el.className).replace(' '+c, '');
            if(elc.indexOf(' ') == 0)
                elc = elc.substring(1);
            el.className = elc;
            return true;
        }
    }
    

    Minified version:

    function hasClass(a,d){var b=" "+a.className+" ";if(b.indexOf(" "+d+" ")<0){return false}return true}function addClass(b,e){if(typeof b.length==="number"&&typeof b[b.length-1]!=="undefined"){for(var a=b.length;a--;){d(b[a],e)}}else{d(b,e)}function d(f,g){if(g.indexOf(" ")>=0){return false}if(hasClass(f,g)){return true}f.className+=" "+g;return true}}function removeClass(b,e){if(typeof b.length==="number"&&typeof b[b.length-1]!=="undefined"){for(var a=b.length;a--;){d(b[a],e)}}else{d(b,e)}function d(f,h){if(h.indexOf(" ")>-1){return false}if(!hasClass(f,h)){return true}var g=(" "+f.className).replace(" "+h,"");if(g.indexOf(" ")==0){g=g.substring(1)}f.className=g;return true}};
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It seems that when the webpage has <meta content='IE=EmulateIE7' http-equiv='X-UA-Compatible' /> so that IE
I am trying to make a webpage that uses a moving div that slowly
Given a webpage that uses lots of javascript to generate its HTML, how can
is that possible to launch a preinstalled application from a webpage. i can see
I am dealing with a webpage that has multiple forms..some basic HTML forms and
I have a webpage that requires login. Once a user has logged in I
I've trained a system on SVM,that is given a question,whether the webpage is a
Given the URL (single line): http://test.example.com/dir/subdir/file.html How can I extract the following parts using
I have a webpage that you pass in an id parameter (via a querystring),
If you were given any random webpage on the internet and had the html

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.