views:

83

answers:

1

When calling an ASP.Net PageMethod, we call it as follows:

function doSomething(htmlElement)
{    
     PageMethods.GetText(onSuccess, onFailure);
}

What is the best way to retain a reference to the htmlElement in the above example, so that we may continue to work with it in the onsuccess method?

Thanks for any help in advance

A: 

Due to the fact that Javascript supports closures, you won't need to worry about maintaining the reference to the element; since it's lexically scoped within onSuccess(assuming you're inlining an unnamed function in the place of onSuccess.)

Simply put, the function you put in for onSuccess can already use the reference to the element as if it were passed in as a parameter.

function doSomething(htmlElement)
{         
    PageMethods.GetText(function(x, y){ var v = htmlElement /*won't be null*/   } , onFailure);

}
Gurdas Nijor
Thanks. Closures - awesome. You are THE man! ;)
James