Wednesday, May 21, 2014

Remove Html tags from a string in Javascript

Strip html tags from a string in javascript. I needed this when I got html formatted address but for google map I required simple address text.

function strip(html)
    {
       var tmp = document.createElement("DIV");
       
        var re = new RegExp('<br>', 'g');
        html = html.replace(re, ' ');
               
        tmp.innerHTML = html;   
       
       
       return tmp.textContent || tmp.innerText || "";
    }


Pass your html string this function will trim the html tags and return it to you. If any break tag present then it will first replace that break with space then send you the text by this way you will find a clean text.

No comments:

Post a Comment