Description of Problem:
I'm attempting to streamline some code, except that I'm obviously just creating a string instead of accessing the value of the respective variable(s). How can I convert 'total_' + animal
into a variable that I can then access to dynamically assign results to the DIVs?
Code:
JS:
function tallyAnimals(animal) {
total_animal++;
if (animal == "dog") { total_dog++; }
if (animal == "cat") { total_cat++; }
if (animal == "bird") { total_bird++; }
if (animal == "fish") { total_fish++; }
//The problem line
$('#' + animal).html('total_' + animal);
}
HTML:
<div id="animals">
<div id="dog">0</div>
<div id="cat">0</div>
<div id="bird">0</div>
<div id="fish">0</div>
</div>