I have the following HTML code on my page. There is no containing element, it’s just in the body.
<b>SqFt per Carton: </b>24.30<br>
Using script, I want to wrap 24.30 in a span tag with a class so the result will look like this:
<b>SqFt per Carton: </b><span class="sqft_cart">24.30</span><br>
How can I do this?
Since you don’t have a containing element you can use
.nextSibling()to get the text node for24.30. Then.insertAdjacentHTML()inserts the newspanafter deleting the old text node. Since I don’t know what the rest of your page looks like, I’ll assume there could be multiple<b>elements, but the code will work either way.Demo: http://jsfiddle.net/ThinkingStiff/6ArzV/
Script:
HTML:
CSS:
Output: