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 6 of 6
  1. #1
    New Coder
    Join Date
    Jan 2005
    Posts
    31
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Identify the element generated the event.

    Hi,
    Is there a way to identify the element which called the javascript function?
    If it is a event, then i can identify from the event object
    If it is like <a href="javascript:test()" /> . Is it possible to identify that it was this anchor element which called the function test. I understand, i could have added a id attribute with some value and could have passed it via test(). There are n number of call in different pages similar to what i have identified above. Hence it will be difficult to find it.

    Thanks,
    Vijay

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #3
    Regular Coder
    Join Date
    May 2005
    Location
    Michigan, USA
    Posts
    566
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You will also need to change
    Code:
    <a href="javascript:test()" />
    to
    Code:
    <a href="javascript:test(event)" />
    To make it work in all browsers.
    Note: I do not test code. I just write it off the top of my head. There might be bugs in it! But if any thing I gave you the overall theory of what you need to accomplish. Also there are plenty of other ways to accomplish this same thing. I just gave one example of it. Other ways might be faster and more efficient.

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    I am thinking of another aproach, using getAttribute method and taking care about the IE/Moz difference:
    PHP Code:
    <script type="text/javascript">
    var 
    myEl=[];
    function 
    searchF(){
    var 
    document.getElementsByTagName('*');
    for(var 
    i=0;i<e.length;i++){
    if(
    e[i].getAttribute('onclick')||e[i].getAttribute('onclick')!=null){// IE/Moz
    var ree[i].getAttribute('onclick').toString().match(/foo()/g);
    re!=null?myEl[myEl.length]=e[i]:null;
    }
    }
    }
    onload searchF;
    </script> 
    Now you have a collection of elements (myEl[]) which trigger onclick the function foo()
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    Kor, you still won't know which particular link was clicked.

    Quote Originally Posted by Vijay Venkat
    There are n number of call in different pages similar to what i have identified above. Hence it will be difficult to find it.
    It's not that difficult. There are many text editors that can "Find and Replace in Files".
    Seach this string:
    Code:
    href="javascript:test()"
    and replace it with:
    Code:
    href="#" onclick="theFunc(this); return false;"
    And you're done. The theFunc() function now has the reference to the link. No more complicated script.

  • #6
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Kor, you still won't know which particular link was clicked.
    It depends on. My guess was that Vijay Venkat needed to know which are the elements as a collection, not as a certain element, at the precise moment of launching the event...

    Even so, there might be a way to add dynamically another function to be triggered onclick... I have studied this for some days, not a final result till now... But maybe this is another thread subject....
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

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