Link to home
Start Free TrialLog in
Avatar of Palamedes
PalamedesFlag for United States of America

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('SomeID');

                        this.Obj.onmousemove = this.tellMeMyId;

                        //eval("this.Obj.onmousemove = " + this.objName + ".tellMeMyId;");  // This doesn't work either
                  }
                  testObject.prototype.tellMeMyId = 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?
ASKER CERTIFIED SOLUTION
Avatar of den_tsopa
den_tsopa

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of den_tsopa
den_tsopa

The IE fix to above example:
               testObject.prototype.tellMeMyId = function(e) {
                    if (e && e.target) {
                        alert(e.target.testObjectInstance.windowId);
                    } else {
                        alert(this.testObjectInstance.windowId);
                    }
               }
Avatar of Palamedes

ASKER

.testObjectInstance isn't a javascript method that I can find.. I've never actually heard of it..

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.
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.
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('SomeID');
                    this.Obj.testObjectInstance = this;
                    this.Obj.onmousemove = function(e)
      {
            this.testObjectInstance.tellMeMyId(e);
      }

                    //eval("this.Obj.onmousemove = " + this.objName + ".tellMeMyId;");  // This doesn't work either
               }
               testObject.prototype.tellMeMyId = function(e) {

                    // THIS WILL FAIL - RETURNS UNDEFINED
                    alert(this.windowId);
               }
               beer_obj = new testObject('beer');
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..



Smaccari;  Your answer is also good btw.. thank you..