Despite what I've read about jQuery/JS scope, this one is still stumping me. Why is the item variable 'undefined' within the inner loop if I define it in the outer loop (or even outside of the outer loop)?
$(".table1 tbody tr").each(function() {
var $self = $(this);
var item = $self.find("td.item").text();
alert(item); //This gives the correct value.
$(".table2 tbody tr").each(function() {
alert(item); //item is undefined here
});
});
I'm hoping the item variable could be passed seamlessly from the outer loop into the inner loop, but clearly I'm missing something. Any explanation would be much appreciated.