By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,793 Members | 1,257 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,793 IT Pros & Developers. It's quick & easy.

Add action listener to different images

P: 1
I am creating an App for iPhone using JavaScript. I have an array of images, let say img[] ,and a place all these images on screen i want to add action listener to each image. Each image has to perform different functionality. I am using this method

Expand|Select|Wrap|Line Numbers
  1. for(var i=0;i<img.length)
  2. {
  3.    img[i].addactionlistner('click',function(e){
  4.     //Print the index of image clicked
  5. });
  6.  
  7.  
  8. }  
This method add action listener to images.Please tell me that this method is right or not. If yes then how can i identify that which image is clicked(it should tell me the index).
May 14 '13 #1
Share this Question
Share on Google+
1 Reply


Dormilich
Expert Mod 5K+
P: 8,639
so far only the for() loop is incorrect. there need to be 2 semi-colons inside.

how can i identify that which image is clicked(it should tell me the index)
usually (i.e. in browser JS), in an event handler this points to the element the handler is executed on (that would be the image element).
if that approach does not apply, you have to use a Closure to get the index into the handler function.
May 14 '13 #2

Post your reply

Sign in to post your reply or Sign up for a free account.