Remove html tag, but keep br Javascript

2

I'm having second thoughts about removing html tags, but keeping only <br>
This code removes the tags, but I would like to keep <br> and maybe other tags.

function removeHtmlTag(strx,chop){ 
    if(strx.indexOf("<")!=-1)
    {
        var s = strx.split("<"); 
        for(var i=0;i<s.length;i++){ 
            if(s[i].indexOf(">")!=-1){ 
                s[i] = s[i].substring(s[i].indexOf(">")+1,s[i].length); 
            } 
        } 
        strx =  s.join(""); 
    }
    chop = (chop < strx.length-1) ? chop : strx.length-2; 
    while(strx.charAt(chop-1)!=' ' && strx.indexOf(' ',chop)!=-1) chop++; 
    strx = strx.substring(0,chop-1); 
    return strx+'...'; 
}
    
asked by anonymous 29.06.2018 / 20:10

1 answer

3

If your code already removes all html tags currently, you can, before passing your function, replace all <br> with a specific string , such as ~br~ , and after it passes through your function, transform all strings ~br~ back to a <br> tag

function trocarTexto() {
    var str = document.getElementById("demo").innerHTML; 
    var res = str.replace("original", "modificado");
    document.getElementById("demo").innerHTML = res;
}
<html>
<body>

<p>Clique no botão para trocar o texto exemplo:</p>

<p id="demo">Texto original!</p>

<button onclick="trocarTexto()">Clique aqui</button>

</body>
</html>
    
29.06.2018 / 20:14