Quantcast
Channel: What's the best way to strip out only the anchor HTML tags in javascript, given a string of html? - Stack Overflow
Viewing all articles
Browse latest Browse all 5

Answer by user113716 for What's the best way to strip out only the anchor HTML tags in javascript, given a string of html?

$
0
0

Here's a native (non-library) solution if performance is a concern.

function stripTag(str, tag) {    var a, parent, div = document.createElement('div');    div.innerHTML = str;    a = div.getElementsByTagName( tag );    while( a[0] ) {        parent = a[0].parentNode;        while (a[0].firstChild) {            parent.insertBefore(a[0].firstChild, a[0]);        }        parent.removeChild(a[0]);    }    return div.innerHTML;}

Use it like this:

alert( stripTag( my_string, 'a' ) );

Viewing all articles
Browse latest Browse all 5

Trending Articles