Node.appendChild Error [closed]
-
20-02-2021 - |
Question
When I retrieve items from my list using REST API, I generate rows to an html table but when I try to append it to a table, I get the following errors:
Uncaught TypeError: Node.appendChild: Argument 1 is not an object.
function onQuerySucceeded(data) {
if(data.d && data.d.results){
var items = data.d.results
var listItems = ''
for (var x = 0; x < items.length; x++) {
listItems += '<tr>'
listItems += '<td>' + items[x].ID + '</td>'
listItems += '<td>' + items[x].ID + '</td>'
listItems += '<td>' + items[x].ID + '</td>'
listItems += '<td>' + items[x].ID + '</td>'
listItems += '</tr>'
}
}
document.getElementById("service").appendChild(listItems) //<---- Error
}
I know it's because listItems is not an object but I'm not sure how get it to where it needs to be.
Any ideas?
Solution
This question is not related to SharePoint, however, you can use HTML DOM innerHTML Property if you want to show constructed HTML inside a DIV.
document.getElementById("service").innerHTML = listItems;
Details on innerHTML: https://www.w3schools.com/jsref/prop_html_innerhtml.asp
OTHER TIPS
Or you could try to use jQuery:
$("#service").append(listItems)
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange