Aniket Singh Aniket Singh - 3 months ago 9
CSS Question

How to remove div with a particulart text in it

I have some div which has some text & elements in it & i want to remove those div, those div look like this

<div style="font-family:verdana;font-size:12px;">Example<a href="http://www.example.com" title="hey">example</a></div>


there are many div like this & i want to remove them with jquery or javascript

Answer

Based on Darren's answer, if you want to be extra sure (as :contains will match and delete any div containing the word example), you can make sure it's a div that has an anchor with that same example as children, then go back to the parent and remove it.

If this doesn't work, please paste a few more divs so we can see a common pattern and target it the safest way possible.

$(document).ready(function(){
  
  $('#remove').click(function(e){

  $("div:contains('Example')").children("a:contains('example')").parent("div:contains('Example')").remove()
})
  
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div style="font-family:verdana;font-size:12px;">Example<a href="http://www.example.com" title="hey"> example</a></div>
<div style="font-family:verdana;font-size:12px;">Don't remove<a href="http://www.example.com" title="hey"> example</a></div>
<div style="font-family:verdana;font-size:12px;">Example<a href="http://www.example.com" title="hey"> don't remove</a></div>

<button id="remove">
Remove undesired divs
</button>