0

I have a class that represents a PhotoFile. It containts attributes, and it can add img elements.

It also containts a static variable that gives each img element (PhotoFile class) created a unique css-class number (like ".no-1", variable called PhotoFile.count).

var PhotoFile = function(){  

   /*attributes */   
   PhotoFile.count = PhotoFile.count+1;    
   this.specificClass = ".no-" + PhotoFile.count;

   /*event listener*/
   this.addClickEvents = function(){

      $("p.listener" + this.specificClass).click( function(){

         console.log("onClick on " + this.SpecificClass + " will fire " + "img" + this.SpecificClass);
         $("img " + this.specificClass).hide();
      });
    }
}

I try to attach a jQuery click events, I register it with the current number of that unique.

If outputs something like this on click (5 - is the number of PhotoFiles in the page):

onClick on .no-5 will fire img.no-5
onClick on .no-5 will fire img.no-5
onClick on .no-5 will fire img.no-5

But when it fires that event, it will always execute the event with the last number, and I assume that it fires the event using last value of PhotoFile.count.

How could I add click events to each object of that class using a unique a unique css-class?

I suppose that when firing elements, it always takes the final static variable. How should I bind elements so events will take the current class static variable?

user2997779
  • 297
  • 1
  • 16

1 Answers1

0

Without seeing some context for the code or a working fiddle his looks like a problem with closure. You want to put the binding to click in a separate function to capture the value. See here for background.

Community
  • 1
  • 1
acarlon
  • 16,764
  • 7
  • 75
  • 94