0

I'm getting data from XML. I can successfully pick up a price from the XML but there is a unexpected error called undefined that shows up when I use the function given below;

<html>
  <head>
    <script type="text/javascript">
      function myXml(origin, destination) {
        var x=xmlDoc.getElementsByTagName("flights");

        for(i=0;i<x.length;i++) {
          if(x[i].getAttribute('FrTLAs')==origin && x[i].getAttribute('destination')==destination) {
            document.write(x[i].getAttribute('price'))
          }
        }
      }
    </script>
  </head>
  <body>
    <script type="text/javascript">
      document.write(myXml('SYD','Bali'));
    </script>
  </body>
</html>
2
  • if xml is not defined so how come my code pick a price from xml. result on browser is 636undefined Commented Apr 14, 2012 at 10:44
  • Not related to your problem, but better declare i as a variable in the local function scope: for(var i=0;.. Commented Apr 14, 2012 at 10:53

2 Answers 2

3

myXml('SYD','Bali') call returns undefined, as you do not return anything in function body. So

document.write(myXml('SYD','Bali'));

will print "undefined" . Just replace above code with this:

myXml('SYD','Bali');
Sign up to request clarification or add additional context in comments.

Comments

1

Engineer is correct, or better return the value from your myXml function.

so, document.write(undefined) wont occur and you may not get the above error.

Comments

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.