I'm trying to remove an em dash from a pre made piece of code. However, it is imbedded in the tree and I cannot seem to access it:
jsFiddle
HTML:
<span class="price">
<span class="price">
<span class="amount">£8.75</span>
– // I need this!
<span class="amount">£19.20</span>
</span>
</span>
JS:
$('span.price').each(function(){
$(this)
.find('.amount')
.next()
.remove()
var c = $(this);
var t = c.text();
var b = t.replace('—', '@'); // but if i use ('£', '@') it replaces the pound sign
c.text(b);
});
Would anyone know why the above does not find and replace the '—' dash?
You can use a combination of .contents() and .filter to find all text nodes whose content is - and remove it:
$(".price").contents().filter(function() {
return this.nodeType == 3 && $.trim(this.textContent) === "–";
}).remove();
jsFiddle
edit: Changed to use jQuery's trim rather than ES5 as this will work on < IE9
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With