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?
LVL 4
PalamedesAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

den_tsopaCommented:
Try 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 = this.tellMeMyId;

                    //eval("this.Obj.onmousemove = " + this.objName + ".tellMeMyId;");  // This doesn't work either
               }
               testObject.prototype.tellMeMyId = function(e) {
                    alert(e.target.testObjectInstance.windowId);
               }
               beer_obj = new testObject('beer');

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
den_tsopaCommented:
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);
                    }
               }
PalamedesAuthor Commented:
.testObjectInstance isn't a javascript method that I can find.. I've never actually heard of it..

So none of those suggestions work.

Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

den_tsopaCommented:
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.
den_tsopaCommented:
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.
smaccariCommented:
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');
PalamedesAuthor Commented:
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..



PalamedesAuthor Commented:
Smaccari;  Your answer is also good btw.. thank you..
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.