MShack MShack - 3 years ago 124
jQuery Question

jquery remove a element if not text is after it

I'm having a real hard time on trying to understand how to remove this class when no text is found after it

example HTML

<ul>
<li>Keep This Text
<a>Keep This Text</a>
</li>
<li>Keep This Text
<a>Keep This Text</a>
</li>
<b class="warning">Comments:</b>Remove preceeding b.warning if no text here
</ul>


I would like it to look like this , when no text is found after the the b.warning

<ul>
<li>Keep This Text
<a>Keep This Text</a>
</li>
<li>Keep This Text
<a>Keep This Text</a>
</li>
</ul>

Answer Source

Just use this row:

if($('b')[0].nextSibling) {
  $('b')[0].nextSibling.nodeValue = '';
  $('b.warning').hide();
}

And here is the working fiddle:

http://jsfiddle.net/cfgr9/532/

Thanks to @nnnnnn and @samuel for their help in comments.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download