javascripthtml-parsing

How to strip HTML tags from string in JavaScript?


How can I strip the HTML from a string in JavaScript?


Solution

  • Using the browser's parser is the probably the best bet in current browsers. The following will work, with the following caveats:

    Code:

    var html = "<p>Some HTML</p>";
    var div = document.createElement("div");
    div.innerHTML = html;
    var text = div.textContent || div.innerText || "";