/    Sign up×
Community /Pin to ProfileBookmark

Dumb question? AJAX return into a table

I’ve never had to write XML data into a table.. I’ve always just populated a div so… maybe this is a dumb question but I’m just not getting it.

I’m running an AJAX query to an ASP page and retrieving rows of data. I want to write the data out in HTML table rows so my first thought is to use a div and innerHTML like I’m familiar with

XML return:
for i=etc..
myrows.innerHTML += <tr>
myrows.innerHTML += <td>1st record name</td>
myrows.innerHTML += <td>1st record address</td>
myrows.innerHTML += <td>1st record phone</td>
myrows.innerHTML += </tr>

(I’ve left out proper formatting since that’s not the issue)

<table>
<tr><th>Name</th><th>Address</th><th>Phone</th></tr>
<div id=myrows></div>
</table>

Well, a div sitting in between table tags is just going to break the table.

What’s the solution?

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@yamaharussauthorJan 13.2008 — Well, I've learned about document.createElement("table"); and got it to work except for one thing.

How do I append the row to the TOP of the table???

Since I am dealing with an interactive list of members online the natural view for this list is having the most recent on top, unlike a chat view where most recent is at the bottom.

function to retrieve results

for (i = 0; i < ... blah

var row = document.createElement("tr");

var cell = document.createElement("td");

var cellText = document.createTextNode(thisvalue);

cell.appendChild(cellText);

row.appendChild(cell);

tblBody.appendChild(row);
Copy linkTweet thisAlerts:
@Orc_ScorcherJan 13.2008 — Try <i>
</i>var row = tblBody.insertRow(0)

instead of<i>
</i>var row = document.createElement("tr");
tblBody.appendChild(row);
×

Success!

Help @yamaharuss spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...