I wish to name an array according to the table row containing the button that was clicked.
I get the table row thus:
var rowNum = $(this).parent().parent().index();
Now, I wish to name the array and access it.
var arrayName = 'arrTR' + rowNum;
window[arrayName] = new Array();
window[arrayName]["First"] = "Bob";
window[arrayName]["Last"] = "Roberts";
window[arrayName]["email"] = "[email protected]";
//The array should be accessible as arrTR__
alert(arrTR1["Last"]);
The alert does not work, so I am doing something wrong.
How should I refactor the code to allow me to update and access the array?
data
method and store it in the$.cache
if you want a global variable. – Johan Sep 13 at 21:48arrTR1
variable. But why are you creating global variables like that? If you explain what you're trying to achieve somebody will suggest a better way. As an aside: don't use arrays with non-numeric properties, you should use objects:window[arrayName] = {}
. – nnnnnn Sep 13 at 22:13data
method and storing in the$.cache
? – gibberish Sep 14 at 3:18data
method, jQuery uses the$.cache
internally. So all you need to know is how to use thedata
method. I've updated my answer – Johan Sep 14 at 17:59