Jquery remove element if there is no text after it

I find it very difficult to figure out how to remove this element 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>
</ul>

<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>Keep b.warning element if text appears after it
</ul>

      

I would like it to look like this when the text after b.warning

not found,
<ul>
   <li>Keep This Text
      <a>Keep This Text</a>
   </li>
   <li>Keep This Text
      <a>Keep This Text</a>
   </li>
</ul>

<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>Keep b.warning element if text appears after it
</ul>

      

+3


source to share


1 answer


Just use this line:

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

      

And here's a working fiddle:



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

Thanks to @nnnnnn and @samuel for helping out in the comments.

+2


source







All Articles