?
Solved

Why does THIS object not know it's own ID?

Posted on 2006-04-13
8
Medium Priority
?
183 Views
Last Modified: 2008-03-17
<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
Comment
Question by:Palamedes
  • 4
  • 3
8 Comments
 
LVL 2

Accepted Solution

by:
den_tsopa earned 200 total points
ID: 16450472
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
 
LVL 2

Expert Comment

by:den_tsopa
ID: 16450512
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
 
LVL 4

Author Comment

by:Palamedes
ID: 16450965
.testObjectInstance isn't a javascript method that I can find.. I've never actually heard of it..

So none of those suggestions work.

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:den_tsopa
ID: 16451002
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
 
LVL 2

Expert Comment

by:den_tsopa
ID: 16451089
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
 
LVL 9

Expert Comment

by:smaccari
ID: 16451817
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
 
LVL 4

Author Comment

by:Palamedes
ID: 16454186
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
 
LVL 4

Author Comment

by:Palamedes
ID: 16455122
Smaccari;  Your answer is also good btw.. thank you..
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question