-4

I have a form thats loads an image. I need to do a function that is been called by the image Onclick Event.

The problem is that the selector ( I'm using jQuery) can't find the image id, because when the function is loaded the image wasn't yet there.

SOLUTION :

After the image upload -->

$("#"+foto_nom).bind({
       click:function(){
       alert(this.id); }
 });

Binding an event is the solution.

Omar
  • 32,302
  • 9
  • 69
  • 112
Despertaweb
  • 1,672
  • 21
  • 29

1 Answers1

4

You should use 'on' method to attach event to selectors that are not present during page load. Like this:

   $(document).on('click', '#id_of_your_image', function(e) {
    //your code here
    });

update

Binding event to the image after it has been added does not seem to be a good solution.

  • 1
    *"Binding event to the image after it has been added does not seem to be a good solution."* Why not? Of course it is dangerous to bind multiple event on one element, but if you are carefull with that, on click, it is actually faster than `.on()`. – Karl-André Gagnon Jul 01 '13 at 15:00