Wednesday, February 3, 2016

Remove HTML tags from string using JavaScript

I encountered a problem in Firefox that it doesn't support InnerText when I try to extract only text from an HTML.
    document.getElementById("mainDiv").InnerText
It works fine in chrome. So tried to remove the HTML tags from the InnerHTML string. Below is the solution for that.

    var html = document.getElementById("mainDiv");

    var text = html.replace(/<\S[^><]*>/g, '');

No comments:

Post a Comment