infomf infomf - 4 months ago 11
jQuery Question

Replace text inside a div

How replace text inside a div with another elements

<strong class="product-detail-final-price">
<input type="hidden" name="priceId" value="2353">
<meta itemprop="price" content="3339">
<meta itemprop="priceCurrency" content="CZK">
<link itemprop="itemCondition" href="https://schema.org/NewCondition">
<meta itemprop="warranty" content="20 let">

3 339 Kč
<span class="pr-list-unit">
/ balení
</span>
</strong>


How edit text
3 339 Kč
? Text is dynamic. Insert
<span>
is not allowed.
Thanks.

Answer

Get the text node using meta tag( with help of nextSibling property ) and update content by textContent property. Attribute equals selector can be used for selecting the meta tag.

$('meta[itemprop="warranty"]')[0].nextSibling.textContent = 'New content';

// or 

document.querySelector('meta[itemprop="warranty"]').nextSibling.textContent 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<strong class="product-detail-final-price">
  <input type="hidden" name="priceId" value="2353">          
  <meta itemprop="price" content="3339">
  <meta itemprop="priceCurrency" content="CZK">
  <link itemprop="itemCondition" href="https://schema.org/NewCondition">
  <meta itemprop="warranty" content="20 let">

  **3 339 Kč**
  <span class="pr-list-unit">
  / balení
  </span>
</strong>

Comments