I have the following code to return a Chinese word's English translation on hover.
//return Chinese/English words from database
$("#npc_dialog span").on("mouseover", function() {
var word = $(this).text();
$("#currentWord").text(parseHoveredText(word));
}).on("mouseout", function() {
$("#currentWord").empty();
});
It works with a for loop:
function parseHoveredText (word) {
for (vocab in hoveredWords) {
if(word.toLowerCase() === vocab) {
return hoveredWords[vocab];
}
}
};
But does not if I use jQuery $.each.
function parseHoveredText (word) {
$.each(hoveredWords, function(key, value) {
if(word.toLowerCase() === key) {
return value;
}
});
};
In both cases, the console.log() of the returned value yields the same value. Why doesn't the jQuery work?
In the for loop:
console.log(hoveredWords[vocab]); gives: You
In the jQuery $.each:
console.log(value); gives: You