Reputation: 22879
I have several fish images, within links, which are listed like so...
<a href="#" class="trigger"><img src="img/fish1.png" id="fish1"/></a>
<a href="#" class="trigger"><img src="img/fish2.png" id="fish2"/></a>
I want to take id 'fish1', 'fish2', ect. (based on which is clicked), and use a variable to replace some text.
The name of the variables are...
var fish1Hunger
var fish2Hunger
And the function I want to call with a variable is...
$(function() {
$('a.trigger').click(function() {
$('#popUp h3').html(REPLACE WITH VARIABLE ASSOCIATED WITH THE FISH);
});
});
How can I call the variable associated with the IMG id?
Upvotes: 1
Views: 290
Reputation: 206121
var fish1Hunger = "Fish 1 is hunger!! Feed him!"
var fish2Hunger = "Fish 2 is hunger!! Feed him!"
$('a.trigger').click(function (e) {
e.preventDefault();
var ImLookingFor = $(this).find('img').attr('id')+'Hunger';
$('#popUp h3').html(eval(ImLookingFor));
});
Upvotes: 0
Reputation: 16210
window[e.srcElement.id+"Hunger"];
Put that inside your click event with e
being the event (function (e){...
) and it should access the variable as long as it is in the global scope.
Upvotes: 2