Raghu Prasad
Raghu Prasad

Reputation: 11

Bootstrap CSS is not working when I create DOM elements dynamically using jquery

<html>
<head>
<title>Bootstrap Grid</title>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<style type="text/css">

</style>
</head> 
<body>
<div class="container">
<div id="Output"></div>
</div>
<script>

$(document).ready(function($) {
    console.log('Document Ready');
    renderData();
    checkoutput();
});
function renderData() {
        var obj = { 'players': [
            { 'fname': 'joe', 'lname': 'smith', 'number': '34' },
            { 'fname': 'jim', 'lname': 'jones', 'number': '12' },
            { 'fname': 'jack', 'lname': 'Hoff', 'number': '84' } 
            ] };

        var cols = GetHeaders(obj); 

        $('#Output').html(CreateTable(obj, cols));
}



    function CreateTable(obj, cols) {
        // below bootstrap table is not effective
        var table = $('<table id="mytable" class="table table-bordered table-condensed table-striped table-hover"></table>');
        console.log('what is there' +table.text());
        var th = $('<tr></tr>');
        for (var i = 0; i < cols.length; i++) {
            th.append('<th>' + cols[i] + '</th>');
        }
        table.append(th);

        for (var j = 0; j < obj.players.length; j++) {
            var player = obj.players[j];
            var tr = $('<tr></tr>');
            for (var k = 0; k < cols.length; k++) {
                var columnName = cols[k];
                tr.append('<td>' + player[columnName] + '</td>');
            }
            table.append(tr);
        }
        return table;
    }

    function GetHeaders(obj) {
        var cols = new Array();
        var p = obj.players[0];
        for (var key in p) {
            cols.push(key);
        }
        return cols;
    }
    function checkoutput(){
    console.log('what is in the html');
    console.log($('#Output').html());
    }

</script>
</body>
</html>

Upvotes: 0

Views: 1429

Answers (2)

Raghu Prasad
Raghu Prasad

Reputation: 11

I got the answer. I have added <tbody></tbody>. After this all bootstrap class added related to table getting executed..

class="table table-bordered table-condensed table-striped table-hover"

var table = $('<table id="mytable" class="table table-bordered 
table-condensed table-striped table-hover"><tbody></tbody></table>');

Upvotes: 1

neophyte
neophyte

Reputation: 6626

<html>
<head>
<title>Bootstrap Grid</title>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<style type="text/css">

</style>
</head> 
<body>
<div class="container">
<div id="Output"></div>
</div>
<script>

$(document).ready(function($) {
    console.log('Document Ready');
    renderData();
    checkoutput();
});
function renderData() {
        var obj = { 'players': [
            { 'fname': 'joe', 'lname': 'smith', 'number': '34' },
            { 'fname': 'jim', 'lname': 'jones', 'number': '12' },
            { 'fname': 'jack', 'lname': 'Hoff', 'number': '84' } 
            ] };

        var cols = GetHeaders(obj); 

        $('#Output').html(CreateTable(obj, cols));
}



    function CreateTable(obj, cols) {
        // below bootstrap table is not effective
        var table = $('<table id="mytable" class="table table-bordered table-condensed table-striped table-hover"></table>');
        console.log('what is there' +table.text());
        var th = $('<tr></tr>');
        for (var i = 0; i < cols.length; i++) {
            th.append('<th>' + cols[i] + '</th>');
        }
        table.append(th);

        for (var j = 0; j < obj.players.length; j++) {
            var player = obj.players[j];
            var tr = $('<tr></tr>');
            for (var k = 0; k < cols.length; k++) {
                var columnName = cols[k];
                tr.append('<td>' + player[columnName] + '</td>');
            }
            table.append(tr);
        }
        return table;
    }

    function GetHeaders(obj) {
        var cols = new Array();
        var p = obj.players[0];
        for (var key in p) {
            cols.push(key);
        }
        return cols;
    }
    function checkoutput(){
    console.log('what is in the html');
    console.log($('#Output').html());
    }

</script>
</body>
</html>

Your bootstrap css is working fine--

You can see bootstrap css is working from red bordered line.

enter image description here

Upvotes: 1

Related Questions