• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 186
  • Last Modified:

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?
0
Palamedes
Asked:
Palamedes
  • 4
  • 3
1 Solution
 
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');
0
 
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);
                    }
               }
0
 
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.

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy 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.
0
 
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.
0
 
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');
0
 
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..



0
 
PalamedesAuthor Commented:
Smaccari;  Your answer is also good btw.. thank you..
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now