Xeptor Xeptor - 3 months ago 9
jQuery Question

Remove specific character and numerical after with jquery

Im trying to remove the the comma and numericals after, but are unsure how. I know how to target it if i knew the characters, like this:

$("em.price.product-card-price").text(function(index, text) {
return text.replace(',62', '');
});


But since the number is dynamic that wont work.

So what is the best way to do this?

<em class="price product-card-price">1090,62€</em>

Answer

Why not use split?

$("em.price.product-card-price").text(function(index, text) {
    return text.split(',')[0];
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<em class="price product-card-price">1090,62€</em>

Maybe you want to add the again.

$("em.price.product-card-price").text(function(index, text) {
    return text.split(',')[0] + text.substr(-1);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<em class="price product-card-price">1090,62€</em>