img.onclick=function{this.attribute=2}

G

gert

obj=function()
{
this.attribute=1
this.method=function()
{
img=document.createElement('img')
img.onclick=function
{
this.attribute=2 // i need the obj this not the img this :)
}
document.body.appendChild(img)
}
}


obj1=new obj()
obj1.method()

Anybody know how to set the obj1 atribute by clicking on a the image ?
 
W

wisestpotato

Try the following:

obj=function()
{
var me = this;
this.attribute=1;
this.method=function()
{
var img=document.createElement('img');
img.src="testimage.gif";
img.onclick=function ()
{
me.attribute=2;
}
document.body.appendChild(img);
}
}

Note that, when the object is created, I store a reference to the
object in a variable called "me". I can then use "me" later on to
refer to the original object, in contexts in which "this" refers to
something else.

I hope that makes sense.

wp.
 
G

gert

Try the following:

obj=function()
{
var me = this;
this.attribute=1;
this.method=function()
{
var img=document.createElement('img');
img.src="testimage.gif";
img.onclick=function ()
{
me.attribute=2;
}
document.body.appendChild(img);
}

}

Note that, when the object is created, I store a reference to the
object in a variable called "me". I can then use "me" later on to
refer to the original object, in contexts in which "this" refers to
something else.

I hope that makes sense.

wp.

great thx :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top