Ingmar Erdös
Ingmar Erdös

Reputation: 557

How to apply css styles to dynamically added content by jQuery

I found this question many times in stackoverflow and on google, but none of the solutions worked for me.

I have a page that loads a html page with empty sections. Then jQuery starts to fill this sections dynamically. The initially loaded html page includes all js and css files and the most of the sections are built using this css files perfectly.

But there is one section, that doesn´t get right. The initially included css file and its contents are not applied to this div tag and its contents...

The project can be found at: HERE

If you scroll down in the left column you´ll find the wordcloud. This words should be viewed in alternating font sizes and colors...

This is the code fragment adding the words:

$.ajax({
    type: "POST",
    url: "the_tagcloud_file.php",
    data: dataString,
    cache: false,
    success: function(html)
    {
        $('#wordcloud').html(html).trigger('create');

    }
});

Could anybody give me an idea or just a hint to the right direction on how to solve this problem?

Thank you in advance for your help!

Best regards,

Ingmar

Upvotes: 2

Views: 7393

Answers (2)

Tech Mahesh
Tech Mahesh

Reputation: 392

You can try this,

$('#wordcloud').html(html).trigger('create');
$('#wordcloud').css( propertyName, value );

For example:

$('#wordcloud').css("color", "red");
$('#wordcloud').css("font-size", "20px");

Upvotes: 0

Jayanth Ramachandran
Jayanth Ramachandran

Reputation: 8716

  • There is no function as such to add this for dynamic . Only way is to , add the css rules dynamically into the body/create a new stylsheet .

Ex:

$("<style>").text("#newId { width:20px; height:30px; }").appendTo("head");

Upvotes: 2

Related Questions