...

View Full Version : need help executing a function and passing a value using a listener



Bobafart
09-28-2007, 12:57 AM
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):



<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?




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);

A1ien51
09-28-2007, 04:46 AM
Here is a basic idea: you can add an onclick event handler to the image



<img onclick="myFunction(this)" ... />


than your function would be



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



You could also add it dynamically such as


document.getElementById("myId").onclick = function(){myFunction(this);}


Eric

Bobafart
09-28-2007, 07:24 PM
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?

BarrMan
09-28-2007, 10:18 PM
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.

GJay
09-29-2007, 01:39 PM
find one of the many wrappers for the different IE/rest event-handling and use that instead, what A1ien51 says still applies. for example:


Event.observe('myID','click',function() {
myFunction(this);
});

(prototype's way of doing it)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum