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
  • 123,204
  • 21
  • 253
  • 243
dchhetri
  • 6,926
  • 4
  • 43
  • 56

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
  • 123,204
  • 21
  • 253
  • 243
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,665
  • 1
  • 19
  • 16
0

This should do the trick:

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

Fidle

Fabricio
  • 839
  • 9
  • 17