-1

I have a button click on which i have to call a webmethod and on success of the method, i need pass the parameter "this" to the function.

 $(".login").click(function () {
     var parent = $(this).parent().parent();
     var id = $(parent).find(".selectbtn").attr("id");
     PageMethods.validateLogin(id, onSuccess, null);
 });

 function onSuccess(result) {
     if (result) {
         //here i need to delete the parent div, that is  var parent = $(this).parent().parent();
     }
 }

Any suggestions?

Esailija
  • 138,174
  • 23
  • 272
  • 326
nimi
  • 5,359
  • 16
  • 58
  • 90

1 Answers1

4

You can inline your function, so that it has access to the local variables:

$(".login").click(function() {
    var parent = $(this).parent().parent();
    var id = $(parent).find(".selectbtn").attr("id");

    PageMethods.validateLogin(id, function (result) {
        if (result) {
            // just use parent here
        }
    }, null);
});

Alternatively, you can pass parent to your onSuccess function:

$(".login").click(function() {
    var parent = $(this).parent().parent();
    var id = $(parent).find(".selectbtn").attr("id");

    PageMethods.validateLogin(id, function(result) {
        onSuccess(result, parent);
    }, null);
});

function onSuccess(result, parent) {
    if (result) {
        // just use parent here
    }
}
Joseph Silber
  • 214,931
  • 59
  • 362
  • 292