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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:13:12+00:00 2026-05-26T01:13:12+00:00

I have a problem. I have a bunch webpage that makes heavy use of

  • 0

I have a problem. I have a bunch webpage that makes heavy use of multiple css classes.

<div class="class1 class2 class3">foo</div>

Unfortunately, I have a “browser” (for lack of a better term) that can not handle multiple css classes in that manner.

I can identify all the elements with multiple classes but now I need to create new classes that merge them. First attempt was to inline all the styles into the style attribute, however that was far too slow, and bloated the document needlessly.

What I now want to do is find an element with multiple classes. Create a new class which is a combination, and replace the elements class with the newly created one, as well as any other elements with the same class combination.

Any thoughts on how best to approach this.

  • 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-26T01:13:13+00:00Added an answer on May 26, 2026 at 1:13 am

    Summary: This function returns an ordered list of all duplicate class names, which can easily be used to merge classes.

    To start off, get a useful list of duplicates:

    var multi = {};
    
    $("*[class]").each(function(){
        var class = this.className.replace(/^\s+|\s+$/g,"").replace(/\s+/g,".");
        if(!/\./.test(class)) return; //Ignore single classes
        if(multi[class]){
            multi[class]++;
        } else {
            multi[class] = 1;
        }
    });
    
    //Now, merge duplicates, because .class1.class2 == .class2.class1
    var multi_nodup = {};
    for(var classes in multi){
        var a_classes = classes.split(".");
        var a_classes = a_classes.sort();
        var a_classes = a_classes.join(".");
        if(multi_nodup[a_classes]){
            multi_nodup[a_classes] += multi[classes];
        } else {
            multi_nodup[a_classes] = multi[classes]
        }
    }
    //Now, multi_npdup is a map of all duplicate classnames
    
    var array_multi = [];
    for(var classes in multi_nodup){
        array_multi.push([multi_nodup[classes], classes]);
    }
    array_multi.sort(function(x,y){return y[0]-x[0]});
    //array_multi is an array which looks like [["class1.class2.class2", 33],
    //             ["class3.class4", 30], ...]
    // = A list, consisting of multiple class names, where multiple classnames
    // are shown, together with the nuber of occurences, sorted according to
    // the frequence
    

    Execute my function, and output variable array_multi. This will show you a map of multiple class names, so that you can replace multiple classnames, accordingly.

    Because of the special way I stored the class names, you can use $("." + array_multi[n][0]) to access all elements which have a set of classname which equals to the set as described at the nth position in array_multi.

    Example of readable output:

    //Overwrites current document!
    var list = "";
    for(var i=0; i<array_multi.length; i++) list += array_multi[i][0] + "\t" + array_multi[i][1];
    document.open();
    document.write("<pre>"+list+"</pre>")
    document.close();
    

    Automatic conversion

    A way to automate the merging of the classnames i by adding all separate class properties to a JavaScript string, and add it to an object. This is the most reliable way to get the exact CSS properties, because attempting to get the classnames through the document.styleSheets object can produce slightly different results. Example:

    var classStyle = {};
    classStyle["class1"] = "border:1px solid #000;";
    classStyle["class2"] = "color:red";
    
    //Make sure that each declaration ends with a semicolon:
    for(var i in classStyle) if(!/;$/.test(classStyle[i])) classStyle[i] += ";";
    
    //Initialise
    var all_styles = {};
    for(var i=0; i<array_multi.length; i++){
        all_styles[array_multi[i][1]] = "";
    }
    
    //This loop takes definition precedence into account
    for(var currentCName in classStyle){
        var currentClass = new RegExp("(?:^|\\.)" + currentCName + "(?:\\.|$)");
    
        // Rare occasion of failure: url("data:image/png,base64;....")
        var separateProps = classStyle[currentCName].split(";");
        var prop_RE = {};
        for(var p=0; p<separateProps.length; p++){
            var cssProperty = separateProps[p];
            if(!/:/.test(cssProperty)) continue; //Invalid CSS property
            prop_RE[cssProperty] = new RegExp("(^|;)\\s*" + cssProperty.match(/(\S+)\s*:/gi)[1] + "\\s*:[^;]+;?", "gi");
        }
    
        for(var class in all_styles){
            if(currentClass.test(class)){
                for(var k in prop_RE){
                    all_styles[class] = all_styles[class].replace(prop_RE[k],"$1") + k;
                }
            }
        }
    }
    
    //To finish off:
    var allClassesToString = "";
    for(var class in all_styles){
        var newClass = class.replace(/\./g, "_");
        $("."+class).each(function(){
            this.className = newClass;
        });
        allClassesToString += "."+newClass + "{" + all_styles[class] + "}\n";
    }
    
    // allClassesToString <------- This variable now holds a string of all duplicate CSS classes!
    //Example:
    var style = $("<style>");
    style.text(allClassesToString);
    style.appendTo($("head:first"));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem. I have a SQL Server table that stores a bunch
We use SharpSVN to programmatically access SVN repositories. Now we have the problem that
The Problem: I have a forms project which instantiates a class that is defined
I have problem in some JavaScript that I am writing where the Switch statement
I have problem with fancybox. I want to write a function that will run
i have problem with autorotate on iphone i set up in all classes -
Problem I have a YQL query result that I'm trying to get converted and
I have a problem committing a bunch of .jar files with eclipse. Maybe eclipse
I have a problem concerning Ajax and Jquery. I have a bunch of <li>
I have the problem when sequentially serialize-deserialize-serialize a TestClass: [Serializable] public class TestClass {

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.