Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find and replace text with jquery, only text no child elements

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 delete the child span sku.

Hope someone has a solution...

like image 711
Maarten Heideman Avatar asked Dec 12 '22 12:12

Maarten Heideman


1 Answers

since jquery 1.8 you can do it also like this:

$(".sku_wrapper").html(function(i,t){
    return t.replace('SKU:','art nu.')
});
like image 52
reyaner Avatar answered Jan 10 '23 21:01

reyaner