Daniel Daniel - 2 months ago 11
jQuery Question

How to highlight in yellow a specific part of a string using jquery

I would like to know how to highlight in yellow only the plus sign (+) of a string using jQuery.


For example:

Text: Lorem ipsum dolor sit amet, consectetur adipiscing elit +

Answer

You can replace a specific part of the string and wrap it. For better handling you should use a class and css for highlighting some parts.

var text = $('#myspan').text();
text = text.replace('+', '<span style="color: yellow;">+</span>');
$('#myspan').html(text)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id="myspan">Lorem ipsum dolor sit amet, consectetur adipiscing elit +</span>

Comments