Add action listener to different images

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • asifbashir
    New Member
    • May 2013
    • 1

    Add action listener to different images

    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

    Code:
    for(var i=0;i<img.length)
    {
       img[i].addactionlistner('click',function(e){
        //Print the index of image clicked
    });
    
    
    }
    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).
    Last edited by Dormilich; May 14 '13, 07:34 PM. Reason: Please use [CODE] [/CODE] tags when posting code.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    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.

    Comment

    Working...