Wednesday, July 25, 2012

Javascript Object not defined after being created in a function



I can\’t seem to return an object from a function, the console.log inside the function prints out the property values fine but once outside the function I\’m getting \”Uncaught ReferenceError: firstOn is not defined\”


Any help would be appreciated, thanks!



myElement = document.getElementById(\"testButton\");

function Server(name,tables,startTime) {
this.name = name;
this.tables = tables;
this.startTime = startTime;
}


document.forms.server1.button.onclick = function() {
var name = document.forms.server1.servername.value;
var tables = document.forms.server1.servertables.value;
var startTime = document.forms.server1.servertime.value;

var firstOn = new Server(name,tables,startTime);

document.forms.server1.button.innerHTML = \"Saved!\";

console.log(firstOn.name);
console.log(firstOn.tables);
console.log(firstOn.startTime);

return firstOn;

};

myElement.onclick = function() {
console.log(firstOn.name);
console.log(firstOn.tables);
console.log(firstOn.startTime);

};






Rating: 5 out of 5 based on 3 ratings



The post Javascript Object not defined after being created in a function appeared first on Javascript ASK.






via Javascript ASK http://javascriptask.phpfogapp.com/javascript-object-not-defined-after-being-created-in-a-function.html

No comments:

Post a Comment