views:

43

answers:

1

Have next script:

function clicker(){
var linkId = "[id*=" + "link]";
var butnId='initial';    
    $j(linkId).click(function(){
     var postfix = $j(this).attr('id').substr(4);
     butnId = '#' + 'butn' + postfix;
     });

return butnId;

}

Function output is 'initial' value.

How to return actual value of variable butnId,after it had been transformed within click function?

+2  A: 

The code inside the function passed to click doesn't get executed until the element is clicked.

You probably need the caller of clicker to pass in a callback function that can then be called by the click function (assuming you don't want to hardcode the required action into the click function itself):-

function clicker(callback){
var linkId = "[id*=" + "link]";
var butnId='initial';    
    $j(linkId).click(function(){
     var postfix = $j(this).attr('id').substr(4);
     butnId = '#' + 'butn' + postfix;
              callback(butnId);
     });

return butnId;

}

Which likely can be refactored to:-

function clicker(callback){
var linkId = "[id*=" + "link]";
    $j(linkId).click(function(){
     var postfix = $j(this).attr('id').substr(4);
     callback('#' + 'butn' + postfix);
     });    
}
AnthonyWJones