Find and replace text with jquery, only text without children

I want to find and replace text with jquery. I want to change "SKU:" to "art nu".

<span itemprop="productID" class="sku_wrapper"> SKU: <span class="sku"> 5-144 </span>. </span> 

I tried:

 $(".product_meta>.sku_wrapper:contains('SKU:')" ).text('art nu.'); 

but this removes the child sku period.

Hope someone has a solution ...

+4
source share
2 answers

since jquery 1.8 you can do it also like this:

 $(".sku_wrapper").html(function(i,t){ return t.replace('SKU:','art nu.') }); 
+12
source

You just tried

 $(".sku_wrapper" ).each(function(){ $this = $(this); $this.html($this.html().replace('SKU:','art nu.')); }); 
+1
source

Source: https://habr.com/ru/post/1496749/


All Articles