Palamedes
asked on
Why does THIS object not know it's own ID?
<html>
<body>
<input id='SomeID' type='button' value="Hover Over Me - and I'll tell you my name" />
<script>
function testObject(wID) {
this.windowId = wID;
this.objName = wID + "_obj";
// Attach events
this.Obj = document.getElementById('S omeID');
this.Obj.onmousemove = this.tellMeMyId;
//eval("this.Obj.onmousemo ve = " + this.objName + ".tellMeMyId;"); // This doesn't work either
}
testObject.prototype.tellM eMyId = function(e) {
// THIS WILL FAIL - RETURNS UNDEFINED
alert(this.windowId);
}
beer_obj = new testObject('beer');
</script>
</body>
</html>
In the code above why does the object not know its own id?
<body>
<input id='SomeID' type='button' value="Hover Over Me - and I'll tell you my name" />
<script>
function testObject(wID) {
this.windowId = wID;
this.objName = wID + "_obj";
// Attach events
this.Obj = document.getElementById('S
this.Obj.onmousemove = this.tellMeMyId;
//eval("this.Obj.onmousemo
}
testObject.prototype.tellM
// THIS WILL FAIL - RETURNS UNDEFINED
alert(this.windowId);
}
beer_obj = new testObject('beer');
</script>
</body>
</html>
In the code above why does the object not know its own id?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
.testObjectInstance isn't a javascript method that I can find.. I've never actually heard of it..
So none of those suggestions work.
So none of those suggestions work.
I've tested my solution - it works in Opera, Firefox and IE (return 'beer' when mouse over input).
testObjectInstance is just a custom field to place owner object reference.
testObjectInstance is just a custom field to place owner object reference.
As far as I know, there is no way to get an object instance which owns handler, inside that handler.
The handler receives event object, which has 'target' field - source object, in your case it is input tag element, and nothing more.
The handler receives event object, which has 'target' field - source object, in your case it is input tag element, and nothing more.
testObjectInstance is a custom property added to the HTML element so as you can reach the JSobject testObject "attached" to it.
(you could call it as you want, such as jsObject or whatever of course).
If you want, in the tellMeMyId function be able to use this by referring the testObject, you can do this:
function testObject(wID) {
this.windowId = wID;
this.objName = wID + "_obj";
// Attach events
this.Obj = document.getElementById('S omeID');
this.Obj.testObjectInstanc e = this;
this.Obj.onmousemove = function(e)
{
this.testObjectInstance.te llMeMyId(e );
}
//eval("this.Obj.onmousemo ve = " + this.objName + ".tellMeMyId;"); // This doesn't work either
}
testObject.prototype.tellM eMyId = function(e) {
// THIS WILL FAIL - RETURNS UNDEFINED
alert(this.windowId);
}
beer_obj = new testObject('beer');
(you could call it as you want, such as jsObject or whatever of course).
If you want, in the tellMeMyId function be able to use this by referring the testObject, you can do this:
function testObject(wID) {
this.windowId = wID;
this.objName = wID + "_obj";
// Attach events
this.Obj = document.getElementById('S
this.Obj.testObjectInstanc
this.Obj.onmousemove = function(e)
{
this.testObjectInstance.te
}
//eval("this.Obj.onmousemo
}
testObject.prototype.tellM
// THIS WILL FAIL - RETURNS UNDEFINED
alert(this.windowId);
}
beer_obj = new testObject('beer');
ASKER
Sorry Den, I spaced and didn't see how you were setting testObjectInstance.. I swear I looked at it 3 times and never saw that line..
ASKER
Smaccari; Your answer is also good btw.. thank you..
testObject.prototype.tellM
if (e && e.target) {
alert(e.target.testObjectI
} else {
alert(this.testObjectInsta
}
}