JohnDoe22366 JohnDoe22366 - 8 months ago 67
jQuery Question

Replace < and > in jquery

I am trying to replace "<" and ">" in jquery in order to display to human being for example.
I'd like it to do it after initialisation.
Any idea?

var el = $("#mydiv").clone();
el.text().replace(new RegExp("<", 'g'), "&lt;");
el.html().replace(new RegExp(">", 'g'), "&gt;");


It would be easier to call .html() on your element and save it to .text();

var el = $("#mydiv").clone();
<script src=""></script>
<div id="mydiv">I'm a <span style="color:red">test</span></div>