Jitender
Jitender

Reputation: 7969

Undefined error in JavaScript

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>

Upvotes: 0

Views: 416

Answers (2)

hitesh israni
hitesh israni

Reputation: 1752

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.

Upvotes: 1

Engineer
Engineer

Reputation: 48813

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');

Upvotes: 3

Related Questions