1

I want to remove/hide a piece of text that loads on my page.

The element doesn't have an id to relate to so I want to use a text-specific method.

Let's say the text is:"remove this line of text".
The html looks like this:

<div class="aClassName">
<p>
    <strong>remove this line of text</strong>
    ... the rest of the content.
</p>

I have tried the following:

jQuery(document).ready(function($){
    document.body.innerHTML = document.body.innerHTML.replace('remove this line of text', '');
});

Didn't work. So I tried this:

jQuery(document).ready(function($){
    $("body").children().each(function () {
       $(this).html( $(this).html().replace(/remove this line of text/g,"") );
    });
});

Didn't work. The idea is that after the page is loaded it removes the line.
It doesn't produces any errors as well. Not even in the firebug.

Anyone?

Interactive
  • 1,474
  • 5
  • 25
  • 57
  • You can remove the DOM element like `$(".aClassName > p > strong:contains('remove this line of text')").remove()` – Satpal May 09 '16 at 13:01
  • http://stackoverflow.com/a/7321960/4478897 – nada May 09 '16 at 13:01
  • Don't re-set entire sections of html, like `body`'s html, as the html will have to be re-parsed and re-rendered – Patrick Evans May 09 '16 at 13:05
  • it would be so much more effective to make a span with an id, class or whatever... searching for content is much longer... depends on your performances needs... – Random May 09 '16 at 13:07

2 Answers2

2

Target Elements Based On Their Content

You could accomplish this using the :contains() pseudoselector in jQuery that would allow you to target certain elements based on their contents :

$(function(){
   // This will remove any strong elements that contain "remove this line of text"
   $('strong:contains("remove this line of text")').remove();
});

You can see a working example of this here.

Broader Approach (Just Targets Elements Based On Selectors)

If you wanted a more simply target it by a more general selector (i.e. any <strong> tags that appear beneath a class called aClassName :

$('.aClassName strong').remove();

You can see an example of this approach here.

Rion Williams
  • 74,820
  • 37
  • 200
  • 327
0

I guess you can use find() and text(), i.e.:

$('.aClassName').find('strong').text("123");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="aClassName">
<p>
    <strong>remove this line of text</strong>
    ... the rest of the content.
</p>

Or simply:

$('strong:contains("remove this line of text")').text("New text");

Update:

After analyzing the link supplied on the comments, you can use the following:

$('.payment_method_mollie_wc_gateway_ideal').find('strong').text("");
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
  • I have tried the following in my header, right before the `` But didn't work. Even the footer didn't work:` ` – Interactive May 09 '16 at 13:11
  • For some strange reason it won't fly. Please check: https://www.cupsandleafs.com/shop/?lang=en and just add something to the cart. When you go to the checkout page; int he payment options it says "Test modus ingeschakeld." I want that line removed! – Interactive May 09 '16 at 13:16
  • No I wish. Then I could just remove this line :-( – Interactive May 09 '16 at 13:26
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/111443/discussion-between-pedro-lobito-and-interactive). – Pedro Lobito May 09 '16 at 13:59