9

So I see here how to add a div and here how to add a class but I'm having trouble combining the two. I want to generate a whole bunch of div's with a specific class and id within the div sparkLineContainer.

I have the containing div

<div id="#sparkLineContainer"></div>

and I want to add a bunch of the following to it

    <div id="#sparkLineContainer">
        <div class="sparkLines" id="id1">Some stuff here</div>
        <div class="sparkLines" id="id2">Some stuff here</div>
        <div class="sparkLines" id="id3">Some stuff here</div>
// and so on
    </div>

snippet - I didn't make it very far, I'm stumped

$('#sparkContainer').add("div");  \\ How do I add the id and class to this div?
                                  \\ And as a repeat the function will it overwrite this?

The function I'm trying to do this with.

function renderSparklines (array1, sparkLineName, id) {
// array1 is the data for the spark line
// sparkLineName is the name of the data.  
// Turn all array values into integers
arrayStringToInt(array1);

    // Create new div of class sparkLines
$('#sparkContainer').add("div")

    // Render new spark line to
$('.sparkLines').sparkline(array1, {width:'90px'});

var htmlString = "";  //  Content to be added to new div
//  GENERATE LITTLE SPARK BOX 
    htmlString += 
                '<font class = "blueDescriptor">' + sparkLineName + '</font>'+
                '<br>'+
                '<font class = "greyDescriptorSmall">Ship to Shore</font>'+
                '<br>'+
                '<font class = "blackDescriptorSparkLine">' + array1[array1.length-1] + '</font>'+
                '<font class = "greenDescriptorSparkline">(' + (array1[array1.length-1] - array1[array1.length-2]) + ')</font>' +
                '<br>';
    $('.sparkLines').prepend(htmlString);

}

1
  • .add() function doesn't add's a div. It just adds to the matched selector. Commented Apr 23, 2012 at 15:25

4 Answers 4

25

add does not do what you think it does. You are looking for append or something similar.

You can create the div first and define its attributes and contents, then append it:

var $newDiv = $("<div/>")   // creates a div element
                 .attr("id", "someID")  // adds the id
                 .addClass("someClass")   // add a class
                 .html("<div>stuff here</div>");

$("#somecontainer").append($newDiv);
Sign up to request clarification or add additional context in comments.

2 Comments

I used the same code but with my div names etc. and no div was added to the content and no error either.
All of the posted answer should work, if they don't then you're doing something wrong in applying them.
9

You need .append or .prepend to add a div to the container. See my version below,

var $sparkLines = $('.sparkLines');
$("#sparkLineContainer")
   .append('<div id="id' + 
            ($sparkLines.length + 1) + 
            '" class="sparkLines">Some Stuff Here</div>')

Also I noticed that you have id of the div as #sparkLineContainer. You should change it as below,

<div id="sparkLineContainer"> 
...

DEMO

4 Comments

I used the same code but with my div names etc. and no div was added to the content and no error either.
@rd42 Works for me jsfiddle.net/skram/KU4Ry/1 . Also in the markup make sure to change the div ID to sparkLineContainer not `#sparkLineContainer'.
Thanks, I was just trying to JSFiddle it
I'm still futzing with it. It works in jsfiddle but not in my code yet. I'm still trying...
3

You can add a div or any other tag along with class like:

$('<div/>',{ class : 'example'}).appendTo("p");

Comments

2

Probably the easiest way is to just modify the innerHTML:

$("#sparkLineContainer").append('<div class="sparkLine" id="id1"></div>');

There's other ways as well, but this is the method I generally use.

4 Comments

Will replace the existing div contents inside #sparkLineContainer. No good!
Modified it to append. I was assuming it was empty at start.
I used the same code but with my div names etc. and no div was added to the content and no error either.
Well in your code you have the container id as "#sparkLineContainer", when it should be just "sparkLineContainer". Not sure if that was a typo on SO though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.