1
<li>
    <a href='http://stackoverflow.com'>Link</a>
    "That was a link"
</li>

How would you, with jQuery, select the "That was a link" string? Obviously using $('li:last') won't work, as :last has to select an element.

So how do you? Let's just say I need to remove it with .remove()

2
  • Can you put a <span> around the text? Commented Oct 26, 2013 at 15:02
  • Nope, that is not possible. Commented Oct 26, 2013 at 15:04

3 Answers 3

1

Try

$($('li')[0].lastChild).remove()

Demo: Fiddle

Sign up to request clarification or add additional context in comments.

Comments

0

You can accomplish this in a few ways. Here's one solution that may not be perfect, but should work:

 <li>
 <a id="myID" href='http://stackoverflow.com'>Link</a>
"That was a link"
 </li>

<script type="text/javascript">

 $(document).ready(function() {

 $('#myID').text();

</script>

You could also change the text of the list item by using this:

$('#myID').text('your text here');

The remove() function is not what you want here. That is used to remove an entire element, not just text.

1 Comment

You do realise that it's the string I am talking about, right? Not the a-tag text.
0

You can use the .contents() method

('li').contents().last().remove()

Demo at http://jsfiddle.net/QJW4N/


Quote

Description: Get the children of each element in the set of matched elements, including text and comment nodes.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.