I am trying to write a javascript function to parse a bit of the DOM tree and return a part of it to further parse. Despite stepping through the function which seems to be working fine, the return of the function shows as undefined to the calling statement. Is there a way to fix this?
from = entityfromid($(value)[0].getElementsByTagName("O1")[0].childNodes[0].childNodes[0].nodeValue).getElementsByTagName("Name")[0].childNodes[0].nodeValue;
function entityfromid(id) {
$($(xmlDoc)[0].getElementsByTagName("Entities")[0].childNodes).each(function (index, value) {
if(value.getElementsByTagName('Id')[0].childNodes[0].nodeValue == id) {
return value;
}
});
}
You need to take the
returnstatement out of the.each(), and instead return from yourentityfromidfunction.Here, when your result is found, it will set the value of the
ret_valuevariable, and do areturn false, which breaks the loop.Then the
ret_valueis used for thereturnfrom your function.