I have a piece of code which dynamically alters the HTML of a div called \'accordion\' on the fly like so:
// htmlstring contains some HTML containing some HTML
Check this link: HTML-encoding lost when attribute read from input field
you can use the function that Anentropic write.
function htmlEscape(str) { return String(str) .replace(/&/g, '&') .replace(/"/g, '"') .replace(/'/g, ''') .replace(//g, '>'); }