I have a situation where I am dynamically building a list of links on a page. When the user clicks on a link I want to call a javascript function passing an object which was created when the page was dynamically built. It appears I am passing the object correctly since when I mouse over the Test link, the browser says [object Object]. The problem is, when the user clicks on the Test link and the testPassObj function is called, a javascript error is thrown saying obj.getFoo is not a function. This indicates to me that obj is not an objectX instance. Any feedback on how I could get this to work would be appreciated.
<html>
<head>
<title>Test</title>
<script type="text/javascript">
function objectX() {
var foo = "";
this.setFoo = function( newFoo ) {
foo = newFoo;
};
this.getFoo = function() {
return foo;
};
}
function testObj() {
var objInstance = new objectX();
objInstance.setFoo( "bar" );
//testPassObj( objInstance ); //-- This works
document.getElementById( "content" ).innerHTML = "<a href='javascript:testPassObj(" + "\\"" + objInstance + "\\"" + ");'>Test</a>";
}
function testPassObj( obj ) {
alert( "testing 2: " + obj.getFoo() ); //-- this should say 'bar'
}
</script>
</head>
<body onLoad="testObj();">
<div id="content"></div>
</body>
</html>