I have a number of check boxes with unique IDs like so
<input id="apple" />
<input id="banana" />
<input id="orange" />
I also have a number of variables with the word ‘Tree’ appended to the end of the ID. I’m able to get the ID when I click the input, but I’d like to return the value of the variable rather than the name that I’ve constructed.
Can someone please point of what’s I’m doing wrong in this code?
$(document).ready(function(){
var appleTree = 1;
var bananaTree = 4;
var orangeTree = 2;
$('input').click(function() {
var ID = $(this).attr('id');
var combinedName = (ID + "Tree");
console.log(combinedName);
});
});
You can create the hashmap as John Green says, or go the evil (but faster) route of eval:
EDIT: I don’t mean literally faster (I have no idea if it is), I just mean less changes.