I have such html:
<li>
<a href="#">2012: Ice Age</a>
<br>
<a href="#"> blah blah </a>
<br>
This text should disappear!!!
</li>
How to I remove that text with Jquery? I don't have a control over this code, so I cannot add any IDs for easier selection..
$('li').contents().last().remove();
If it is at the end, you can use the contents()
[docs] method (which gets all children, including text nodes, and the last()
[docs] method to target the last one.
Example: http://jsfiddle.net/KtTfQ/
EDIT:
You could also just empty the content of that text node:
$('li br:last-child')[0].nextSibling.nodeValue = '';
Example: http://jsfiddle.net/KtTfQ/2/
It's easiest done with plain old DOM manipulation:
var text = $("li").get(0).lastChild;
text.parentNode.removeChild(text);
Fiddle: http://jsfiddle.net/LBxqa/1/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With