tags:

views:

25

answers:

1

This code doesn't work

jQuery.each(["Alloggio","Macchina","Aereo","Treno"], function(){         
        t = this;
        $("#ChkPrenotazione" + t + "Default").change(function(){            
            $(".Chk" + t).val($(this).val());        
        });
    });

I want that t inner on change event is equal to "Alloggio" or "Macchina" or "Aereo" or "Treno"

How can i fix it?

thanks

+1  A: 

You need to store the t locally. This can be done with a closure:

jQuery.each(["Alloggio","Macchina","Aereo","Treno"], function(){         
    $("#ChkPrenotazione" + this + "Default").change((function(t){
        return function() {
            $(".Chk" + t).val($(this).val());
        };
    })(this));
});

Here we use the function function(t) { … } to return a function with our corresponding this. This is done by calling that function with this as the parameter for t.

Gumbo