JS. How to replace html element with another element/text, represented in string?

Posted by EL 2002 on Stack Overflow See other posts from Stack Overflow or by EL 2002
Published on 2012-11-14T22:18:53Z Indexed on 2012/11/14 23:00 UTC
Read the original article Hit count: 196

Filed under:
|
|
|

I have a problem with replacing html elements.

For example, there is a table

<table><tr><td id="idTABLE">0</td><td>END</td></tr></table>

(it can be div, span, anything)

And string in JS script

var str='<td>1</td><td>2</td>';

(it can be anything, '123 text', '<span>123 element</span> 456' or ' <tr><td>123</td> ' or anything)

How can I replace element 'idTABLE' with str? I mean really replace, so

<table><tr><td id="__TABLE__">0</td><td>END</td></tr></table>

becomes

<table><tr><td>1</td><td>2</td><td>END</td></tr></table> 
//str='<td>1</td><td>2</td>';

<table><tr>123 text<td>END</td></tr></table>
//'123 text'

<table><tr> tr><td>123</td> <td>END</td></tr></table>
//' <tr><td>123</td> '

I tried with createElement, replaceChild, cloneNode, but with no result at all =(

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about html