MartinDK81 MartinDK81 - 5 months ago 8
jQuery Question

Select and replace dynamic text

The question is simple: How would you select the below text and change it? The only rule is that it will always start with {name



<body>
<table>
<tr>
<td>
{name length='100'}
</td>
</tr>
</table>
</body>




Answer

If you want to replace the whole inner text that starts with {name, try this:

function replace(newVal) {
    var tds = document.getElementsByTagName("td");
    for (var i = 0; i < tds.length; i++) {
        var elem = tds[i];
        if ( elem.innerText.startsWith("{name") ) {
            elem.innerText = newVal;
        }
    }
}

replace("new-value");
<body>
  <table>
    <tr>
      <td>
        {name length='100'}
      </td>
    </tr>
  </table>
</body>

Comments