By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,623 Members | 1,968 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,623 IT Pros & Developers. It's quick & easy.

html to object

100+
P: 107
Hey guys,

I have a script that gets a table row from an ajax post and needs to append it to a table. Im wondering if there is away to turn the plain html that is returned into an object. Heres a simplified example

Expand|Select|Wrap|Line Numbers
  1.  
  2. ajaxResult = "<tr><td>test</td></tr>";
  3.  
  4. objTable = document.getElementById("mytable");
  5.  
  6. newTR = turnHTMLintoObject(ajaxResult);
  7.  
  8. objTable.appendChild(newTR);
  9.  
  10.  

I thought of a couple of easy work arounds:

1. I could return just the "<td>test</td>" bit then insert that into a new TR object with newTR.innerHTML = "<td>test</td>";

this means more code to set the attributes of the <tr> tag which would have been set by the php backend.

2. I could append the row to the table like so
objTable.innerHTML += ajaxResult;

But im wondering if there is away to turn the html directly into an object, which would be more helpful for other things i may want to do with the code before appending.

Thanks in advance!


Andy
Jun 19 '08 #1
Share this Question
Share on Google+
1 Reply


acoder
Expert Mod 15k+
P: 16,027
DOMParser's parseFromString method can parse XML/XHTML, but not HTML for which you're stuck with innerHTML.
Jun 19 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.