1

I wish to access a variable named storedata while dynamically changing the objects event onclick event handler

function update_data(storedata)
{
    obj.onclick=function() {remover(this,storedata); };
}

Not able to access storedata within the inner function.

When I try using alert(obj.onclick) the result comes out to be remover(this,storedata). It seems the variable is not been replaced by its value!!

Any Ideas?

2 Answers 2

2

It is "replaced". The problem is just the way you are trying to inspect it.

When you alert a function, you get a stringified version of the function … and that includes the variable names and not serialisations of every variable.

If you were to actually call the function, the variable would be available with the value you gave it.

Sign up to request clarification or add additional context in comments.

Comments

0

You need to attach it as a property of the object which has got the event handler anyhow. When you storedata is a string and you are using HTML5, you could for example accomplish it that way:

function update_data(storedata){
   obj.dataStoredata = storedata;
   obj.onclick = function(){
      remover(this);
    };
}

Inside of your remover function them you can simply access this.dataStoredata which will contain your data.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.