I have this code:
function get_id_from_coords (x, y)
{
x = parseInt(x);
y = parseInt(y);
if (x < 0)
{
x = (x + 6) * 60;
}
else
{
x = (x + 5) * 60;
}
if (y < 0)
{
y = (y + 6) * 60;
}
else
{
y = (y + 5) * 60;
}
$('#planets').children().each(function(){
if ($(this).attr('x') == x) {
if ($(this).attr('y') == y) {
alert (parseInt($(this).attr('id')));
return parseInt($(this).attr('id'));
}
}
});
}
alert(get_id_from_coords(x, y));
However, from this code I get two popups:
First, from inside the function, I get the proper value (like 63), but then, when I alert the return value, I just get undefined.
You get undefined as you function doesn’t return – the last statement is a call to
eachfunction and it’s not areturnstatement. If you put a return, e.g.it will return something – in this case, based on the docs:
http://api.jquery.com/jQuery.each/
it will return the children of
#planets.If you want to find some value specifically using
each, then you can do something like this: