#1
  1. User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Location
    UK
    Posts
    471
    Rep Power
    76

    Lightbulb GetElementsByTagName


    Hi everyone,
    I'm pretty new to this whole new style DOM javascript and I was looking for some help into document.getElementsByTagName.

    What I want to do is take every link on the page
    document.getElementsByTagName("a")
    and then take there href and alter it so it includes a function, like so:
    Before JS:
    <a href = "test.php">HEY!</a>
    After JS:
    <a href = "javascript:dosomething('test.php');">HEY!</a>

    dosomething(str) is my function which runs a nifty little ajax script but i'm just wondering how to do the above using document.getElementsByTagName (unless there is a better less complicated way of doing it).

    Thanks,
    Sephers.
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2006
    Location
    England
    Posts
    397
    Rep Power
    74
    Code:
    var allLinks = document.getElementsByTagName( 'a' );
    
    for(var i=0, len=allLinks.length; i<len; i++ )
    {
     allLinks[ i ].onclick = function()
     {
       doSomething( this.href );
       return false;
     };
    }

    Comments on this post

    • sephers181 agrees : I like you code ;)


    No it's not 'awesome' - it's just code.
  4. #3
  5. User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Location
    UK
    Posts
    471
    Rep Power
    76
    Thanks, that works really great.

IMN logo majestic logo threadwatch logo seochat tools logo