Search code examples
jqueryhtml-escape-characters

What is the reverse operation of jQuery text()


I have a user input like

var input = "x + y > z"

I create an element like this:

var span = jQuery("<span/>").text(input)

How can I get the original text back, not escaped?


Solution

  • Using .text() will give you the text content. That will not be an escaped version. Whereas if you use .html() then the text would be escaped and returned.

    var span = jQuery("<span/>").text(input)
    var returnedText = span.text();