Lemvig Kiggeren
Lemvig Kiggeren

Reputation: 317

Select a string inside an element jQuery

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

Upvotes: 1

Views: 99

Answers (3)

Gabriele Petrioli
Gabriele Petrioli

Reputation: 196306

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.

Upvotes: 0

psarid
psarid

Reputation: 450

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.

Upvotes: 0

Arun P Johny
Arun P Johny

Reputation: 388446

Try

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

Demo: Fiddle

Upvotes: 1

Related Questions