Javascript code removing anchor tags

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • smita232
    New Member
    • Nov 2008
    • 5

    Javascript code removing anchor tags

    HI ,

    i have a code :

    <a href="#" >Hyperlink</a>


    Now how do i remove the anchor tag dynamically using Javascript?
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Find the link in the DOM (document.getEl ementsByTagName () or getElementById( ) if it has an ID) and then use removeChild:
    Code:
    document.body.removeChild(..)

    Comment

    • smita232
      New Member
      • Nov 2008
      • 5

      #3
      Thanks for the reply....but could you plz give a more detailed description of the code .

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        explaind by MDC: removeChild() - MDC

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          If you don't know the parent node, you can use the parentNode property:
          Code:
          node.parentNode.removeChild(node);
          where node is the element to be removed.

          Comment

          • Stano Kobella
            New Member
            • Aug 2011
            • 1

            #6
            As people say, this will do it:
            Code:
            $(document).ready(function() {
             var a=document.getElementsByTagName('a');
             for(var i=0;i<a.length;i++)
              if ((a[i].attributes['href'] && a[i].attributes['href'].value=='#') && (a[i].innerHTML=='Hyperlink'))
               a[i].parentNode.removeChild(a[i]);
            });

            Comment

            Working...