Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing the text before an element with jQuery

Imagine this scenario:

<span>Item 1</span> | <span>Item 2</span>

How can I target the | and remove it? Also, assume I always need to remove the | before the span with "Item 2" in it, and the list can grow with items being added before OR after "Item 2." All new items will be enclosed within span and they'll be separated by |.

like image 515
user1729506 Avatar asked Jan 30 '26 19:01

user1729506


1 Answers

$('span').each(function() {
    if ($(this).text() == 'Item 2') {
        $(this.previousSibling).remove();
    }
});

http://jsfiddle.net/spFUG/2/

like image 178
dersvenhesse Avatar answered Feb 01 '26 11:02

dersvenhesse



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!