M
mikevanoo
Hi,
Can anyone work out why the following doesn't work. For some reason,
when you dynamically add a button to the document from a Javascript
object it can't then reference the properties of the object like a
"normal" button. Any ideas?
Thanks in advance,
Mike.
===================================================================
<html>
<head>
<script type="text/javascript">
var obj;
function TestObject () {
this.ID = "testObject1";
this.Init = TestObject_Init;
this.Test = TestObject_Test;
}
function TestObject_Init() {
this.Button = document.createElement("INPUT");
this.Button.type = "button";
this.Button.value = "Dynamically Added Button - this should display
\"testObject1\" but doesn't!";
this.Button.onclick = this.Test;
document.body.appendChild(this.Button);
}
function TestObject_Test() {
alert(this.ID);
}
function Go() {
obj = new TestObject();
obj.Init();
}
</script>
</head>
<body onload="Go()">
<input type="button" value="Static Button - This works as it should."
onclick="obj.Test();" />
</body>
</html>
===================================================================
Can anyone work out why the following doesn't work. For some reason,
when you dynamically add a button to the document from a Javascript
object it can't then reference the properties of the object like a
"normal" button. Any ideas?
Thanks in advance,
Mike.
===================================================================
<html>
<head>
<script type="text/javascript">
var obj;
function TestObject () {
this.ID = "testObject1";
this.Init = TestObject_Init;
this.Test = TestObject_Test;
}
function TestObject_Init() {
this.Button = document.createElement("INPUT");
this.Button.type = "button";
this.Button.value = "Dynamically Added Button - this should display
\"testObject1\" but doesn't!";
this.Button.onclick = this.Test;
document.body.appendChild(this.Button);
}
function TestObject_Test() {
alert(this.ID);
}
function Go() {
obj = new TestObject();
obj.Init();
}
</script>
</head>
<body onload="Go()">
<input type="button" value="Static Button - This works as it should."
onclick="obj.Test();" />
</body>
</html>
===================================================================