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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:46:34+00:00 2026-06-17T22:46:34+00:00

My script needs to dynamically create a stylesheet in the section of the page

  • 0

My script needs to dynamically create a stylesheet in the section of the page (rather than put inline styles on each element, because I need to override these styles with media queries later on).

The Code

for (var i=0; i<theElements.length; i++){

    $(theElements[i]).not('.responsive-wrap').each(function(i, elem){
        var theWidth = $(this).width();
        var parentWidth = $(this).parent().width();

        $(this).addClass('element' + [i]);

        $("#dynamicStylesheet").text('.element' + [i] + ' {max-width: ' + theWidth + 'px;}');
    });
}

The main question is:

This completely overrides the text in the #dynamicStylesheet each run of the loop (so when I load the page there is only one rule for .element22). How can I make it ADD the text without overwriting?

Sub-questions for bonus points:

  • This works for the most-part, but ONLY when [i] is in square
    brackets. Why is this?

    Do I need the for loop here, or is .each(function(){}) essentially
    creating a for loop anyway? There’s another each function below this one inside the for loop that I haven’t posted to keep it succinct.

  • 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-17T22:46:35+00:00Added an answer on June 17, 2026 at 10:46 pm

    Okay, so, a few things. Assuming that theElements is an array of HTML elements, the each is unnecessary. But that doesn’t seem to be the case, since you say element22 is showing up (your code as its written would only create element0 over and over again if theElements was an array of HTML elements). So apparently theElements is an array of arrays, or an array of jQuery objects. Which is weird given its name, but let’s power through.

    As others have mentioned, you’re overwriting the text every iteration, which is bad. Building a string is slightly better, but you’re still doing repeated string concatenation, which is slow. If we had to do this, we should append to an array and then join it together at the end (which is faster, since appending to a string creates the entire string from scratch every time).

    var css = [];
    for (var i = 0; i < theElements.length; i++) {
        $(theElements[i]).not('.responsive-wrap').each(function(i) {
            var theWidth = $(this).width();
            $(this).addClass('element' + i);
            css.push('.element' + i + ' {max-width: ' + theWidth + 'px;}');
        });
    }
    $("#dynamicStylesheet").text(css.join('\n'));
    

    Now, that works, and will do what you want. But this is almost definitely not the right way to do whatever it is you’re trying to do. Dynamically building CSS on the client is not good, unless you’re writing something like a CSS editor. There are much easier ways to solve whatever problem led you down this path.

    If creating a client-side stylesheet is absolutely vital, then there are better ways to do this, too. Create a single CSS rule that applies to all the elements of the elements of theElements (wow, look at that sentence). That’s not possible if they’re chosen randomly, but if you arrived at them through some logical query you can use the same one in CSS.

    As far as the [i] thing goes — I don’t know what to tell you. Due to the weird way JavaScript stringifies arrays, they’re equivalent, and if i doesn’t work for you then there’s something very wrong. I would be using i.toString() if I were you, but i should work fine.

    Edit

    If theElements is an array of tag names, this can be simplified to:

    var tagNames = theElements; // clear variable names are important!
    var css = [];
    $(tagNames.join(',')).not('.responsive-wrap').each(function(i) {
        var $this = $(this);
        $this.addClass('element' + i.toString());
        css.push('.element' + i.toString() + '{ max-width: ' + $this.width() + 'px; }');
    });
    $("#dynamicStylesheet").text(css.join('\n'));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a Python script that needs to create about 50 distinct temporary
for example I have a script that needs to put it's parent directory on
I am dynamically adding tabs to my page using the following script: var tabs
I need to create form dynamically from Django and I need a script to
How would you create a new html li element dynamically in the code behind
I need to create multiple tweet buttons, after the page has loaded. Each button
I need to show for every link, dynamically some text with Java-Script (jQuery). The
Perl script needs to receive ajax request, send a success message back to browser,
My perl script needs to ask the user for a password to download some
Im working on a script that needs to find a match for certain letters(chords)

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.