SOLDIER-OF-FORTUNE
SOLDIER-OF-FORTUNE

Reputation: 1654

wrapping div around string using jquery

How do i wrap a div class div class="includingVAT"></div> around the string text :incl. MwSt

<span id="VariantPrice_3181">
<span class="variantprice">
<span class="pricelabel">Preis&nbsp;</span>
240,00 (CHF)
</span>
&nbsp;incl. MwSt
<span id="plus-shipping">plus-shipping</span>
</span>

using jquery?

it needs to look like this:

<span id="VariantPrice_3181">
<span class="variantprice">
<span class="pricelabel">Preis&nbsp;</span>
240,00 (CHF)
</span>
<div class="includingVAT">&nbsp;incl. MwSt</div>
<span id="plus-shipping">plus-shipping</span>
</span>

the string without span or div needs the div class includingVAT as there could be different language translations.

Upvotes: 0

Views: 740

Answers (2)

Aesthete
Aesthete

Reputation: 18848

$(".pricelabel").after('<div class="includingVAT">');
$(".plus-shipping").before('</div>');

This is awkward and error-prone. Is there any-way you can insert the whole div? Why does incl. MwSt exist only in the HTML?

Upvotes: 2

Niklas
Niklas

Reputation: 13145

You can try this example here: DEMO

var text = $('#plus-shipping').map(function(){
    return this.previousSibling.nodeValue
});
$('#plus-shipping').prepend('<div class="includingVAT">' + text[0] + '</div>');

Upvotes: 2

Related Questions