Apparently, this is harder to find than I thought it would be. And it even is so simple...
Is there a function equivalent to PHP\'s htmlspecialchars built into Javas
I am elaborating a bit on o.k.w.'s answer.
You can use the browser's DOM functions for that.
var utils = {
dummy: document.createElement('div'),
escapeHTML: function(s) {
this.dummy.textContent = s
return this.dummy.innerHTML
}
}
utils.escapeHTML('&')
This returns <escapeThis>&
It uses the standard function createElement to create an invisible element, then uses the function textContent to set any string as its content and then innerHTML to get the content in its HTML representation.