Here is what I am trying to do
Fox<span>Rox</span>
before that
FoxRox
How do I wrap spans around Rox
?
jQuery wrap() method is used to wrap specified HTML elements around each selected element. The wrap () function can accept any string or object that could be passed through the $() factory function. Syntax: $(selector). wrap(wrappingElement,function(index))
Wrap a set of elements Select the element or elements that you want to enclose in a div. Use one of the following methods to wrap the selected elements: Select Edit > Wrap from the top menu.
Use the textContent property to get the text of a span element, e.g. const text = span. textContent . The textContent property will return the text content of the span and its descendants. If the element is empty, an empty string is returned.
This will search you whole DOM and replace each Rox
with <span>Rox</span>
:
$(':contains("Rox")').html(function(index, oldHTML) {
return oldHTML.replace(/(Rox)/g, '<span>$&</span>');
});
Live DEMO
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