mheavers
mheavers

Reputation: 30158

javascript / jquery : problem calling a function inside an object (function undefined)

I'm getting an error of "buildXML is not defined" when I run this code:

var c = {

  updateConsumer:function (cid,aid,sid,survey){
    var surveyXML = buildSurveyXML(survey);
  },

  buildSurveyXML: function(survey) { 
    var surveyResults = survey.split("|");
    var surveyXML = '';

    for (var i=0;i<surveyResults.length;i++){
       ...
    }

    return surveyXML;
  }
}

And the html that includes this JS and calls the updateConsumer function:

<!DOCTYPE HTML>
<html lang="en">
    <head>
        <title>Web Service Test</title>
        <meta charset="utf-8">
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.6.2.min.js"></script>
        <script type="text/javascript" src="../../shared/js/consumerSoap.js"></script>
    </head>

    <body>
        <script type="text/javascript">
             $(document).ready(function() {
              c.insertConsumer("First","Last","55555","[email protected]","76:1139");
             });
        </script>
    </body>
</html>

Upvotes: 0

Views: 159

Answers (2)

Niko
Niko

Reputation: 26730

Use var surveyXML = c.buildSurveyXML(survey);

Upvotes: 0

NT3RP
NT3RP

Reputation: 15370

The problem is that updateConsumer doesn't know anything about buildSurveyXML; that function isn't in the global scope. However, since your function is part of the same object, you can call it using the this keyword.

updateConsumer:function (cid,aid,sid,survey){
    var surveyXML = this.buildSurveyXML(survey);
}

Upvotes: 2

Related Questions