Search code examples
jqueryarrayslive

How to access array in jQuery live event?


I have the following code:

var numberOfSelectedOptions = new Array();
numberOfSelectedOptions[0]=0;
numberOfSelectedOptions[1]=0;
numberOfSelectedOptions[2]=0;

$("a.tagvariantoption").live("click", function(){
 alert(numberOfSelectedOptions[2]);
});

The alert always says "undefined". It works perfectly when alerting outside of the live event though. Any ideas on why my array is undefined or unknown within the live event?

Heres some extra info:

var startcount = 0; 
var numberOfSelectedOptions = new Array(); 
numberOfSelectedOptions[0]=0; 
numberOfSelectedOptions[1]=0; 
numberOfSelectedOptions[2]=0; 

$("a.tagvariantoption").live("click", function(){ 
  alert(startcount); //gives 0 
  alert(numberOfSelectedOptions[0]); //gives undefined??? 
)};

The HTML is working because startcount is printed correctly, but my array stays unknown within the event.


Solution

  • I solved it somehow like this:

    var selectedOptionsCounter = new Array(0,0,0);

    from then on, that array is accessible in the live event, no idea what was wrong before though.