cgrouge
cgrouge

Reputation: 177

jquery find and replace < with ">"

I'm attempting to use jquery to find &lt; or &gt; and replace it with < or >. The idea is to change this from a text element into a live div tag.

I have this jsfiddle but it doesn't seem to be replacing those characters correctly because I still see "" in the output.

$('#myText').each(function() {
      var text = $(this).text();
      text = text.replace("&lt;", "/\</g");
      text = text.replace("&gt;", "/\>/g");
      $(this).text(text).clone().appendTo("#output").css("color", "blue");
      preventDefault;
    });

Why isn't this turning into a live div?

Upvotes: 0

Views: 458

Answers (2)

AMS
AMS

Reputation: 104

Because you are using .text() function and .text() is equivalent to innerText in standard javascript, try to use .html(), which is equivalent to innerHTML, instead and most probably it will work.

Upvotes: 1

stackoverfloweth
stackoverfloweth

Reputation: 6917

try changing .text() to use .html()

$(this).html(text).clone().appendTo("#output").css("color", "blue");

Upvotes: 4

Related Questions