Reputation: 402
I'm trying to hide the text from the paragraphs. The problem is that the script find the text and remove the whole paragraph. I want to remove only that text that is searched for. Demo
<div class='article'>
<p>Text may refer to: Contents. 1 Computers and electronics; 2 Arts and entertainment; 3 See also. Text (journal), an academic journal of language, discourse, and...</p>
</div>
$(function() {
var foundin = $('.article p:contains("journal")');
$(foundin).hide();
});
Upvotes: 0
Views: 112
Reputation: 874
$(function() {
$('.article p:contains("journal")').each(function(){
$(this).html($(this).html().replace(/journal/g, ""))
});
});
Upvotes: 1
Reputation: 422
var foundin = $('.article p:contains("journal")');
foundin.text(foundin.text().replace(/journal/g, ""))
Upvotes: 4
Reputation: 318182
Wrap the text in hidden spans with a class
$(function() {
var text = 'journal';
$('.article p').html(function(_,html) {
return html.replace(new RegExp('('+text+')','g'), '<span class="hidden" style="display:none">$1</span>')
});
});
to show it again you can do
$('.hidden').show();
Upvotes: 1
Reputation: 18292
$(function() {
var foundin = $('.article p:contains("journal")');
var html = foundin.html();
html = html.replace('journal', '<span class="hidden">journal</span>');
$(foundin).html(html);
});
And the CSS
.hidden
{
display: none;
}
This way, by changing the .hidden
style you can highlight the text instead hiding it. This solution won't affect the markup of the rest of the paragraph.
Upvotes: 1