Is there a way to define variables in javascript to append them with another variable?
var card_id;
var card_links_grid+card_id;
I'd like the second variable to be appended with the information found in the first variable. For each card_id there will be a card_links_grid. I want to be able to access the card_links_grid variable for a specific card_id.
What's the right way to do that?
The right was is to use either an array or an object, depending on if the id is going to be sequential or named.
var card_links_grid = {};
var card_id = "the_hanged_man";
card_links_grid[card_id] = "some value";
or
var card_links_grid = [];
card_links_grid.push("some value");
Variable variables are possible, but only with some hard to maintain and debug approaches that should be avoided. Use object types designed for the data structure you want to represent.
I also recommend to use either array or object. But if you want to try with variable variable approach, you can try like this.
var card_id=1;
window['card_links_grid_' + card_id] = 'Marco';
document.write(window['card_links_grid_' + card_id]);
http://jsfiddle.net/Q2rVH/
Ref: http://www.i-marco.nl/weblog/archive/2007/06/14/variable_variables_in_javascri
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With