Search code examples
javascripthtmltagshtml-entities

Reading html content with without escaping?


I am trying to read the html content using innerHTML. It seems to convert the '<' in the text content to '&lt;' .

I would rather not to. Is there anything in JS that would allow me to render as such


Solution

  • Lodash supports an unescape() function that does this.

    For example,

    // This reads < as &lt;
    let html = document.querySelector('.el').innerHTML
    // This converts the &lt; back into <
    html = _.unescape(html)
    

    More answers are available in this question: Unescape HTML entities in JavaScript?