Remove links with Javascript

Posted by Arlen Beiler on Stack Overflow See other posts from Stack Overflow or by Arlen Beiler
Published on 2010-03-31T19:56:20Z Indexed on 2010/03/31 20:03 UTC
Read the original article Hit count: 243

Filed under:
|

How do I remove links from a webpage with Javascript. I am using Google Chrome. The code I tried is:

function removehyperlinks() {
    try {
        alert(document.anchors.length);
        alert(document.getElementsByTagName('a'));
        for(i=0;i=document.anchors.length;i++) {
            var a = document.anchors[i];
            a.outerHTML = a.innerHTML;
            var b = document.getElementsByTagName('a');
            b[i].outerHTML = b[i].innerHTML;
        }
    } catch(e) { alert (e);}
    alert('done');
}

Of course, this is test code, which is why I have the alerts and 2 things trying at the same time. The first alert returns "0" the second [Object NodeList] and the third returns "done".

My html body looks like this:

<body onload="removehyperlinks()">
<ol style="text-align:left;" class="messagelist">
    <li class="accesscode"><a href="#">General information, Updates, &amp;   Meetings<span class="extnumber">141133#</span></a>
        <ol>
            <li><a href="#">...</a></li>
            <li><a href="#">...</a></li>
            <li><a href="#">...</a></li>
            <li><a href="#">...</a></li>
            <li><a href="#">...</a></li>
            <li><a href="#">...</a></li>
            <li><a href="#">...</a></li>
            <li><a href="#">...</a></li>
            <li start="77"><a href="#"">...</a></li>
            <li start="88"><a href="#">...</a></li>
            <li start="99"><a href="#">...</a></li>
        </ol>
    </li>
  </ol>
</body>

© Stack Overflow or respective owner

Related posts about html

Related posts about JavaScript