views:

139

answers:

1

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.

A: 

Hi guys,

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.

Jorre
What browser were you using to test?
Jeff Sternal
Um suddenly you array has a different name numberOfSelectedOptions vs selectedOptionsCounter?
jitter
`var selectedOptionsCounter = [0,0,0];` is what you should be doing...
gnarf