DEV Community

Nagarajan R
Nagarajan R

Posted on

Answer: Is there an easy way to style the last couple characters of a string?

Off the top of my head in jQuery:

<script type="text/javascript">
$('.numbers').each(function() {
    $(this).html(
        $(this).html().substr(0, $(this).html().length-2)
          + "<span style='color: #3399ff'>"
          + $(this).html().substr(-2)
          + "</span>");
});
</script>

Here's a fiddle to demonstrate it. I'm sorry it's not plain JavaScript, but I'm sure one of the folks here can offer a native solution…

Top comments (0)