How to apply style sheets to dynamically created elements?

Kai

this should be an easy one: I have the following code:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link href="css/bootstrap.min.css" rel="stylesheet">
 <script type="text/javascript" src="js/jquery-2.1.4.min.js"></script>
  <script type="text/javascript">
    $(document).ready(function(){
        var table = document.createElement("table");
        table.setAttribute("id","table");
        var headRow = document.createElement("tr");
        var dataRow = document.createElement("tr");
        var head = document.createElement("th");
        var data = document.createElement("td");
        head.innerHTML="head";
        data.innerHTML="data";
        headRow.appendChild(head);
        dataRow.appendChild(data);
        table.appendChild(headRow);
        table.appendChild(dataRow);
        console.log(document.styleSheets);
        table.className = "table";
        document.getElementById("test").appendChild(table);
    });
  </script>
</head>
<body>
<div id="test"></div>
</body>
</html>

The problem is: The style specified in the bootstrap.min for the class "table" is never applied upon loading the side. Do I have to do something additionally? I test it in the most recent version of Safari.

Any Help?

Cheers Twerp

Praveen Kumar Purushothaman

It is not dynamically created issue. It is because you haven't set the class to table. As Bootstrap Docs says:

You have to use <thead> and <tbody> for the class to work.

To add in your answer:

var thead = document.createElement("thead");
thead.appendChild(headRow);
var tbody = document.createElement("tbody");
tbody.appendChild(dataRow);

Add this for a quick fix:

$("tr:first-child").wrap("<thead/>");

Your final code should look like:

    var table = document.createElement("table");
    table.setAttribute("id","table");
    var headRow = document.createElement("tr");
    var dataRow = document.createElement("tr");
    var head = document.createElement("th");
    var data = document.createElement("td");
    var thead = document.createElement("thead");
    thead.appendChild(headRow);
    var tbody = document.createElement("tbody");
    tbody.appendChild(dataRow);
    head.innerHTML="head";
    data.innerHTML="data";
    headRow.appendChild(head);
    dataRow.appendChild(data);
    table.appendChild(thead);
    table.appendChild(tbody);
    console.log(document.styleSheets);
    table.className = "table";
    document.getElementById("test").appendChild(table);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

CSS will not apply to dynamically created elements in jQuery mobile

From Dev

Apply jQueryUI Resizable widget to dynamically created elements

From Dev

CSS will not apply to dynamically created elements in jQuery mobile

From Dev

Changing style for children elements of dynamically created jQuery

From Dev

How to add 'style' in dynamically created table?

From Dev

How to apply style to the divs that are added dynamically?

From Dev

Is there a way to apply jQuery to dynamically created elements? (not event listeners)

From Dev

PrototypeJS: how to select dynamically created elements?

From Dev

How to use @FormParam if form elements are dynamically created

From Dev

How to wrap two dynamically created elements with span?

From Dev

How to select deeper elements created dynamically?

From Dev

Apply style to ALL elements

From Dev

Apply style to ALL elements

From Dev

Styling elements created dynamically

From Dev

getElementByClassName for dynamically created elements

From Dev

How To Access Dynamically Created Page Elements with jQuery (not events, ELEMENTS!)

From Dev

how to apply auto complete for dynamically created input box

From Dev

How to apply jQuery File Uploader (blueimp) on dynamically created inputs

From Dev

How to apply jQuery File Uploader (blueimp) on dynamically created inputs

From Dev

CSS not apply for dynamically created html

From Dev

Style a dynamically created div element

From Dev

jQuery How to add/remove dynamically created elements from dynamically created divs?

From Dev

How can I apply CSS to elements created with javascript in IE?

From Dev

How to apply self-created MDG profile to existing elements in model

From Dev

How to properly apply CSS style to H elements in ordered list

From Dev

Where are dynamically created elements stored and how to access them? jQuery

From Dev

How to add an event handler for dynamically created QML elements?

From Dev

How to create a listener for dynamically created elements with ember-cli

From Dev

how do i make dynamically created elements draggable()?

Related Related

  1. 1

    CSS will not apply to dynamically created elements in jQuery mobile

  2. 2

    Apply jQueryUI Resizable widget to dynamically created elements

  3. 3

    CSS will not apply to dynamically created elements in jQuery mobile

  4. 4

    Changing style for children elements of dynamically created jQuery

  5. 5

    How to add 'style' in dynamically created table?

  6. 6

    How to apply style to the divs that are added dynamically?

  7. 7

    Is there a way to apply jQuery to dynamically created elements? (not event listeners)

  8. 8

    PrototypeJS: how to select dynamically created elements?

  9. 9

    How to use @FormParam if form elements are dynamically created

  10. 10

    How to wrap two dynamically created elements with span?

  11. 11

    How to select deeper elements created dynamically?

  12. 12

    Apply style to ALL elements

  13. 13

    Apply style to ALL elements

  14. 14

    Styling elements created dynamically

  15. 15

    getElementByClassName for dynamically created elements

  16. 16

    How To Access Dynamically Created Page Elements with jQuery (not events, ELEMENTS!)

  17. 17

    how to apply auto complete for dynamically created input box

  18. 18

    How to apply jQuery File Uploader (blueimp) on dynamically created inputs

  19. 19

    How to apply jQuery File Uploader (blueimp) on dynamically created inputs

  20. 20

    CSS not apply for dynamically created html

  21. 21

    Style a dynamically created div element

  22. 22

    jQuery How to add/remove dynamically created elements from dynamically created divs?

  23. 23

    How can I apply CSS to elements created with javascript in IE?

  24. 24

    How to apply self-created MDG profile to existing elements in model

  25. 25

    How to properly apply CSS style to H elements in ordered list

  26. 26

    Where are dynamically created elements stored and how to access them? jQuery

  27. 27

    How to add an event handler for dynamically created QML elements?

  28. 28

    How to create a listener for dynamically created elements with ember-cli

  29. 29

    how do i make dynamically created elements draggable()?

HotTag

Archive