Q:

can not use Closure right

I wrote JS function and it must bind the buttons it generates depending on values in the array.
But it gives me the last value. I read that i had to use closure, I did, and I'm still not able to bind them right!
I'm still a beginner 
I read about closure, I got the idea but still did not know what I'm missing
function addNewServices(newServicesArray){
    var j=0; var x;
    for (i in newServicesArray){
        var html='';

        html='<div style="width: 33%; float: leftt"><a href="#" data-role="button" data-icon="home" id="btn-'+newServicesArray[j].servicename+'" value="'+newServicesArray[j].servicename+'" class="ui-btn-up-c">'+newServicesArray[j].servicename+'</a></div>';
        $("#main-menu").append(html);

        $('#btn-'+newServicesArray[j].servicename).bind('click', function (){bindThis(j)});
        j++;
    }

    var bindThis = function( j ) {
        return function() {
            alert(j); // gives 2 always
            alert( newServicesArray[j].servicename ); 
        };
    };
}

A:

you don't have to bind click in a loop... you can get the clicked refrence  by $(this) in a function..
making it as simple as i can..
function addNewServices(newServicesArray){
   var j=0; 
   for (i in newServicesArray){
      var html='';

      html='<div style="width: 33%; float: left"><a href="#" data-role="button" data-icon="home" id="btn-'+newServicesArray[j].servicename+'" value="'+newServicesArray[j].servicename+'" class="ui-btn-up-c">'+newServicesArray[j].servicename+'</a></div>';

      $("#main-menu").append(html);

   }
}

$(function(){
  $(document).on('click','a[id^="btn-"]',function (){
      var $this = $(this);
      alert($this.attr('value')); 
  });
});

