I'm a newbie in JavaScript, I use a piece of code to convert JSON to HTML table.
And here is the JavaScript code:
function buildHtmlTable(myList,printSource,tablename) {
var columns = addAllColumnHeaders(myList);
var title = document.getElementsByTagName("caption");
title.innerHTML="<h>"+printSource+"</h>";
for (var i = 0 ; i < myList.length ; i++) {
var row$ = $('<tr/>');
for (var colIndex = 0 ; colIndex < columns.length ; colIndex++) {
var cellValue = myList[i][columns[colIndex]];
if (cellValue == null) { cellValue = ""; }
row$.append($('<td/>').html(cellValue));
}
$("#excelDataTable").append(row$);
}
}
// Adds a header row to the table and returns the set of columns.
// Need to do union of keys from all records as some records may not contain
// all records
function addAllColumnHeaders(myList) {
var columnSet = [];
var headerTr$ = $('<tr/>');
for (var i = 0 ; i < myList.length ; i++) {
var rowHash = myList[i];
for (var key in rowHash) {
if ($.inArray(key, columnSet) == -1){
columnSet.push(key);
headerTr$.append($('<th/>').html(key));
}
}
}
$("#excelDataTable").append(headerTr$);
return columnSet;
}
Here is my HTML code:
<body onload="buildHtmlTable(data_epgd, epgd);">
<table id="excelDataTable" border="1">
<caption>sn</caption>
</table>
</body>
As you can see, there is a usage of $("#excelDataTable")
. I thought it's just like document.getElementById
function to find an element in HTML.So I use document.getElementById
to replace it. But when I did this, the code didn't work any more.So can somebody explain what is the difference between document.getElementById(excelDataTable)
and $("#excelDataTable")
? And why I can't use document.getElementById(excelDataTable)
as alternative?