Aariba Aariba - 2 months ago 7
HTML Question

Find Text from content then hide using jQuery

I am trying hide

specific
text element from content by jquery. Example below.

HTML:

<div id="name">
Trying hide specific Text elements from content.
</div>


I want this by jquery:

<div id="name">
Trying hide <p style="display: none;">specific<p> Text elements from content.
</div>


There any simple solution by jquery?

Answer

You can use .html() method.

Also, use regex so you could hide all of them.

Like this:

var hideElm = 'specific',
    regex = new RegExp(hideElm, 'g');

$('#name').html(function(i, html){
  return html.replace(regex, '<span style="display:none">' + hideElm + '</span>');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="name">
Trying hide specific Text elements from content. Another specific
</div>