JohnDoe22366 JohnDoe22366 - 4 years ago 169
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;");

Answer Source

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>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download