Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

The 'this' value in event handling object method. 1

Status
Not open for further replies.

lenrobert

Programmer
Feb 24, 2005
13
GB
Hi everybody! I got stuck in a problem:

If I call a method of a self-defined object the "this" value usually equals to the self-defined object. However in the case of an event handling method I cannot get back the object. Can anyone help me?

Code:
<html>
<head>
<script>

function Class1() {
    this.choose = Choice;
    this.Name;
}

function Choice(e) {
  if (this.Name) alert(this.Name+" - Good!");        // this is what I want always
  else if (this.tagName) alert(this.tagName+"  :("); // but this is what I get when 
                                                     // event is handled.
}

var Obj1 = new Class1();
Obj1.Name = "Object1";

</script>
<title>This</title>
</head>

<body onload="document.F1.S0.onchange=Obj1.choose">

<form method="GET" action="anything.cgi" name="F1">
    <select size="2" name="S0">
    <option>Option1</option>
    <option>Option2</option>
    </select>
</form>

<a href="javascript:Obj1.choose()">Obj.choose() - the nice case</a>


</body>
</html>
 
Obj1.choose is simply a pointer to the Choice function and the "this" keyword refers to whatever object calls that function. You just have to make sure your object calls the function and not the SELECT. Change your body tag to this:
Code:
<body onload="document.F1.S0.onchange=function(){Obj1.choose()}">

Adam

for(ring=0;ring<rosie;ring++){pocket[ring]='posie';ashes*=2;fall--}
 
Another way:
Code:
<select size="2" name="S0" onchange="Obj1.choose()">
    <option>Option1</option>
    <option>Option2</option>
</select>

Adam

for(ring=0;ring<rosie;ring++){pocket[ring]='posie';ashes*=2;fall--}
 
Hi Adam, the first one works excellently (because onchange is assigned by a script in my project), thank you for your quick and valuable reply.

Robert
 
Adam, can you suggest me some reading about this topic (execution contexts and other advanced material)? I am struggling with "ECMAScript Language Specification" but it is not too good, is there any better? Thanks in advance, Robert.
 
I haven't seen any reading material about this particular topic, it's just something I've picked up along the way. But if you want to learn advanced JavaScript, you'll want to know about Object Hierarchy and Inheritance in JavaScript. You'll need a good Quick Reference guide like DevGuru. Regular expressions can be a valuable addition to JavaScript. You can learn about them at Regexlib and Regular Expressions.info. You'll want to reverse engineer some sites that use advanced techniques like WebFX and the DynAPI. I can't think of any other sites off the top of my head. Maybe some others can add their favorites here.

Oh, and of course, you should go to a bookstore and browse through the JavaScript books. Remember, a thicker book doesn't neccessarily mean it's better. I think I read one of the "JavaScript Unleashed" books a long time ago and found that there was a lot of stuff that I already knew, but I learned some cool stuff too.

Adam

for(ring=0;ring<rosie;ring++){pocket[ring]='posie';ashes*=2;fall--}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top