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/