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.

5
  • 1
    You could probably start by not adding an ID that stars with a number css-tricks.com/ids-cannot-start-with-a-number Commented Oct 8, 2013 at 20:46
  • @Ollie yea that was just an example, but good to know Commented Oct 8, 2013 at 20:47
  • 1
    @Ollie, ID's starting with numbers is ok in HTML5 - stackoverflow.com/questions/70579/… Commented Oct 8, 2013 at 20:48
  • @Sergio: this is absolutely true, they are, however, still a pain to select in CSS. Commented Oct 8, 2013 at 20:53
  • @DavidThomas, true, good remark. Commented Oct 8, 2013 at 20:55

3 Answers 3

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';
Sign up to request clarification or add additional context in comments.

Comments

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')

Comments

0

This should do the trick:

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

Fidle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.