Search code examples
javascriptjquerystringifyouterhtml

Extract outerHTML variable from $table


I have not been able to find any information about this, so forgive the lack of knowledge. I created a table using the code below, but now I want to use the table with a jQuery plugin but in order to do it I need to print out the table, but that I mean

<table>
  <tr>
  .
  .
  .

I am not sure how to only get the text. I tried printing it $table to the console, but it gives me the object and its properties. Part of the object is the outerHTML, which is exactly what I need. Can I extract this outerHTML? Can I stringify or print the text for $table without being an object?

Thanks

var columns = ["username","user_id","address","state","postal_code","phone","email"];

var level_classes = {"NEW":"new_client", "RENEWAL":"renewing_client", "CURRENT":"current_client"};

$(document).ready( function() {
    $.getJSON("obtainUsers.php", function(data) {
        var $table = $('<table style="width: 100%;">');
        var $tbody = $('<tbody>');
        $table.append($tbody);
        var $tr = null;

        data.forEach(function(user, index){
            if(index % 4 === 0) {
                $tr = $('<tr>');
                $tbody.append($tr);
            }
            $td = $('<td class="'+level_classes[user.level]+'">');
            columns.forEach(function(col){
                $td.append(user[col]);
                $td.append($('<br>'));
            });
            $tr.append($td);
        });
        $('.runninglist').append($table);
    });
});

Solution

  • use .html() to print the outerHTML node. Thanks @Ted.