1

Suppose I have this:

<li id="0">
  <i class="abc"></i>
  TEST TEXT
</li>

How could I update "TEST TEXT" with some other string? Using $("#0").text('NEW TEXT') removes the i-tag.

PSL
  • 122,084
  • 19
  • 250
  • 241
dchhetri
  • 6,590
  • 4
  • 39
  • 55

3 Answers3

3

You can do:

$('#0').find('i')[0].nextSibling.nodeValue = 'NEW TEXT';

or since it is the last child then:

$('#0')[0].lastChild.nodeValue = 'NEW TEXT'; //you can just use document.getElementById

or

document.getElementById('0').lastChild.nodeValue = 'NEW TEXT';
PSL
  • 122,084
  • 19
  • 250
  • 241
1

wrap the "test text" in a span, and replace that.

<li id="0">
  <i class="abc"></i>
  <span id="replaceme">TEST TEXT</span>
</li>

$("#replaceme").text('NEW TEXT')
nebulae
  • 2,645
  • 1
  • 18
  • 16
0

This should do the trick:

$("#0").text(function() {
    $(this).children().html() + "NEW TEXT");
});

Fidle

Fabricio
  • 829
  • 9
  • 17