Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find specific string in a div and delete its html element

is there a way using javascript to find a specific string and delete its' html element. For example, I have this following html code:

<div class="summary">
List of texts:
  <ul>
   <li>Text 0</li>
   <li>Text 1</li>
   <li>Text 2</li>
   <li>Text 3</li> 
   <li>Text 4</li>
   <li>Text 5</li>
  </ul>
</div>

and I want to hide "Text 2", first I want to find this string and then hide it. what I've tried is using html().replace but it hides only the text not the element. JSFiddle

like image 440
Khalifa Sabt Avatar asked Feb 23 '26 03:02

Khalifa Sabt


2 Answers

In jQuery there is :contains

$('.summary li:contains(Text 2)').hide();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="summary">
List of texts:
  <ul>
   <li>Text 0</li>
   <li>Text 1</li>
   <li>Text 2</li>
   <li>Text 3</li> 
   <li>Text 4</li>
   <li>Text 5</li>
  </ul>
</div>
like image 192
Oleksandr T. Avatar answered Feb 25 '26 16:02

Oleksandr T.


Your fiddle is pretty close. Here's an updated version removing the element from the DOM.

$('.summary li').each(function() {
   var $this = $(this);
   if ($this.text() === 'Text 2') {
       $this.remove();
   }
});

http://jsfiddle.net/sLa3dkdd/3/

like image 40
grgdne Avatar answered Feb 25 '26 17:02

grgdne



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!