Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Access a href elements from Javascript

    Hi,

    I have a requirement to list out a number of values as links (dynamically).
    Onclicking any of these links , the value of this link should be set in a hidden form variable from javascript.

    For eg: if the list of links are

    abc1234
    pqr12334
    fgf23344
    (The number of values in this list is dynamic)
    I should be able to get abc1234 from the hidden variable if I click on the first link.

    Pls help
    Thanks

  • #2
    Regular Coder
    Join Date
    Dec 2008
    Location
    Tannhäuser Gate
    Posts
    286
    Thanks
    7
    Thanked 58 Times in 57 Posts
    Quote Originally Posted by anki2610 View Post
    list out a number of values as links (dynamically).
    Onclicking any of these links , the value of this link should be set in a hidden form variable from javascript.
    That's exactly your assignment or are there more details?

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,035
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    I don't think that it is possible to capture the text content of a link (but happy to be proved wrong).
    This is probably the best you can do:-


    Code:
    <a href="#" onclick = "myFunction('abc12345')">abc12345</a>
    <a href="#" onclick = "myFunction('pqr12344')">pqr12344</a>
    <a href="#" onclick = "myFunction('fgf23344')">fgf23344</a>
    
    <script type = "text/javascript">
    
    function myFunction (which) {
    alert (which);
    }

    "The only thing to do with good advice is pass it on. It is never any use to oneself." - Oscar Wilde (Irish Poet, Novelist, Dramatist and Critic, 1854-1900)

  • #4
    Regular Coder
    Join Date
    Dec 2008
    Location
    Tannhäuser Gate
    Posts
    286
    Thanks
    7
    Thanked 58 Times in 57 Posts
    Code:
    <body>
    <a href="#" onclick = "myFunction('abc12345')">abc12345</a>
    <a href="#" onclick = "myFunction('pqr12344')">pqr12344</a>
    <a href="#" onclick = "myFunction('fgf23344')">fgf23344</a><br />
    <script type = "text/javascript">
    var a=document.body.getElementsByTagName('a');
    for(i=0;i<a.length;i++){
    document.write(a[i].innerHTML+'<br />')
    }
    </script>
    </body>

  • #5
    Regular Coder
    Join Date
    Feb 2009
    Location
    Wheeling, IL
    Posts
    358
    Thanks
    5
    Thanked 62 Times in 60 Posts
    Or.....

    Code:
    <script type="text/javascript">
        window.onload = function() {
            var temp = document.getElementsByTagName("a");
                for (i=0;i< temp.length;i++) {
                    temp[i].onclick = clicky;
                }
        }
        function clicky() {
            var blah = this.innerHTML;
            alert(blah);
            return false;
        }
    </script>
    Just replace the alert(marked in red) with whatever you want it to do with the variable... and you should be all set. Each click on a link on the document will do whatever you need it doing. Enjoy.

    EDIT: This was tested with the following HTML mark-up:

    Code:
    <a href="#">Test 1</a>
    <a href="#">Something else</a>
    <a href="#">Oh my god!</a>
    P.S. Philip, read my signature.
    Last edited by Eldarrion; 03-17-2009 at 05:21 PM.
    The way to success is to assume that there are no impossible things. After all, if you think something is impossible, you will not even try to do it.

    How to ask smart questions?

  • Users who have thanked Eldarrion for this post:

    Philip M (03-17-2009)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •