html to object

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • theS70RM
    New Member
    • Jul 2007
    • 107

    html to object

    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

    Code:
    ajaxResult = "<tr><td>test</td></tr>";
    
    objTable = document.getElementById("mytable");
    
    newTR = turnHTMLintoObject(ajaxResult);
    
    objTable.appendChild(newTR);

    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.innerH TML += 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
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    DOMParser's parseFromString method can parse XML/XHTML, but not HTML for which you're stuck with innerHTML.

    Comment

    Working...