Element Links

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mirek
    New Member
    • Jun 2007
    • 2

    Element Links

    Hi.

    How can I get all links for a specified element?

    For example, how can I extract links from:

    <td id="test">
    <a href = "www.google.com ">Google</a>
    <a href = "www.yahoo.com" >Yahoo</a>
    </td>

    Please help. Thanks in advance :)
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    #2
    Originally posted by Mirek
    Hi.

    How can I get all links for a specified element?

    For example, how can I extract links from:

    <td id="test">
    <a href = "www.google.com ">Google</a>
    <a href = "www.yahoo.com" >Yahoo</a>
    </td>

    Please help. Thanks in advance :)
    Welcome to TSDN.

    I m writing this code for IE, for others u ll try urself.

    [code=javascript]
    var TD_ref = document.getEle mentById("test" );
    for(var i=0;i<TD_ref.ch ildren.length;i ++)
    {
    if(TD_ref.child ren[i].tagName == "a") //ur_code to do with the links.
    }
    [/code]

    Best of luck.

    Kind regards,
    dmjpro.

    Comment

    • Mirek
      New Member
      • Jun 2007
      • 2

      #3
      thank you for your reply, but this doesn't work neither on firefox neither on IE:

      Code:
      <html>
      <head>
      <title>getElementById example</title>
      
      <script type="text/javascript">
      
      function f1()
      {
            var TD_ref = document.getElementById("id1");
            alert("was here 1");
            for(var i=0;i<TD_ref.children.length;i++)
            {
               if(TD_ref.children[i].tagName == "a") //ur_code to do with the links.
                alert("was here 2");
            }
      }
      </script>
      </head>
      
      <body>
      <div id="id1"><a href ="http://www.google.com">Google</a></div>
      <button onClick="f1();">blue</button>
      </body>
      </html>

      Comment

      • mrhoo
        Contributor
        • Jun 2006
        • 428

        #4
        var atags=
        document.getEle mentById('test' ).getElementsBy TagName('a');

        /*
        atags returns a node list of <a> elements;
        if you want to return an array of href's run a loop on atag's members:
        */
        var A=[], tem;
        var L= atags.length;

        for(var i=0;i<L;i++){
        tem=atags[i];
        if(tem.href)A.p ush(tem.href);
        }
        //return A;

        Comment

        Working...