I realise that I can prepend stuff to an element using:
$(...).prepend(myText);
However, if myText
is, let’s say, "<span>"
, I actually want that text to appear, but .prepend()
would instead prepend an empty span element. What is the recommended way to solve this? Do I really have to HTML-escape the text manually or is there something more elegant?
You can create a textnode and put the contents there and prepend
that:
$('div').prepend(document.createTextNode("<span>"));
example: http://jsfiddle.net/niklasvh/gCKHe/
You can use the text
function instead of prepend
, and simply add the original text to the end of the new text:
$("#elementID").text("<span>" + $("#elementID").text());
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