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
    Regular Coder
    Join Date
    Dec 2006
    Posts
    417
    Thanks
    168
    Thanked 1 Time in 1 Post

    Post need help executing a function and passing a value using a listener

    I need help executing a function and passing a value using a listener.

    If my mark up is a series of images for instance (icons in this case):
    Code:
    <img id="imgActivate" src="path/to/png1">
    <img id="imgActivate" src="path/to/png2">
    <img id="imgActivate" src="path/to/png3">
    <img id="imgActivate" src="path/to/png4">
    how would I create the code to set up a Listener to execute my function and pass a unique value to that function depending on which image icon was clicked?

    Code:
    function myFunc(e){
    
           what code goes in here to know which icon was clicked? -- ie. how do I pass that value so that I can tell which image was clicked? (png1, png2, png3 or png5???)  
          
    }
    obj.Listener( "imgActivate", myFunc, true);

  • #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
    Here is a basic idea: you can add an onclick event handler to the image

    Code:
    <img onclick="myFunction(this)" ... />
    than your function would be

    Code:
    function myFunction(elem){
      alert(elem.src);
      alert(elem.id);
    }

    You could also add it dynamically such as
    Code:
    document.getElementById("myId").onclick = function(){myFunction(this);}
    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • Users who have thanked A1ien51 for this post:

    Bobafart (09-28-2007)

  • #3
    Regular Coder
    Join Date
    Dec 2006
    Posts
    417
    Thanks
    168
    Thanked 1 Time in 1 Post
    I appreciate your help friend.

    However, I am purposely trying to avoid using onClick's.

    I was hoping there was an efficient, clean and unobtrusive way to do it using a Listener.

    --
    Besides.. aren't onClick's passe?

  • #4
    Senior Coder BarrMan's Avatar
    Join Date
    Feb 2005
    Location
    Israel.
    Posts
    1,644
    Thanks
    69
    Thanked 83 Times in 82 Posts
    What's wrong with onclick event? Why avoid it? It doesn't cause any trouble or is considered a low leveled coding.
    There's no reason that I know of, for you to avoid using the onclick event.

  • #5
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    find one of the many wrappers for the different IE/rest event-handling and use that instead, what A1ien51 says still applies. for example:
    Code:
    Event.observe('myID','click',function() {
      myFunction(this);
    });
    (prototype's way of doing it)
    My thoughts on some things: http://codemeetsmusic.com
    And my scrapbook of cool things: http://gjones.tumblr.com


  •  

    Posting Permissions

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