0

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;
    }
});
}
1
  • 1
    Please post your actual code. Commented Jul 5, 2011 at 16:32

1 Answer 1

3

You need to take the return statement out of the .each(), and instead return from your entityfromid function.

function entityfromid(id) {

    var ret_value;

    $($(xmlDoc)[0].getElementsByTagName("Entities")[0].childNodes).each(function (index, value) {
        if(value.getElementsByTagName('Id')[0].childNodes[0].nodeValue == id) {
            ret_value = value;
            return false;
        }
    });
    return ret_value;
}

Here, when your result is found, it will set the value of the ret_value variable, and do a return false, which breaks the loop.

Then the ret_value is used for the return from your function.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Didn't expect returning from there to cause it to not properly return from the function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.