I am creating a whole HTML table, dynamically from Javascript. That includes also the table heading creation dynamically, and I want each table heading to contain some text. So i want to achieve something like this:
<th>#</th>
<th>City</th>
<th>State</th>
<th>Region</th>
The way I am generating this from Javascript, for the single column heading, is the following, but it doesnt work. So when I do that, I only get a <th></th>
HTML, with no text inside.
function createTableHeaders(table) {
// get my row template
var table_row = $($("#my_table_row").html());
// get my table column heading template
var table_header = $($("#my_table_col_heading").html());
// insert some text to the column heading
// none of these below work ...
table_header.innerHTML = "City";
table_header.innerText = "City";
table_header.text = "City";
// append table column heading to table row with headings
table_row.append(table_header);
// append the entire row to the table
table.append(table_row);
}
On a side note, I would also like to be able to add some custom CSS to the text that I want to have in the column heading. How can I do this?
HTML templates
<!-- Define table -->
<script type="text" id="my_table">
<table class="a-bordered a-align-center a-spacing-base a-size-base"></table>
</script>
<!-- Define table heading element: each column has it's own heading -->
<script type="text" id="my_table_col_heading">
<th class="a-color-base a-size-base a-text-center"></th>
</script>
<!-- Define table row -->
<script type="text" id="my_table_row">
<tr class="a-spacing-base a-spacing-top-base a-text-center"></tr>
</script>