$(function(){ for(i = 1; i < 4; i++){ var aux = 'aux'+ i; aux_state(aux); } }); function aux_state(aux){ $.get(host +'/get_aux/' + aux, function(state){ $('#'+ aux).switchbutton({ checked: parseInt(state), onChange: function(checked){ $.get(host +'/set_aux/'+ aux +'/'+ checked, function(data){ // do perl stuff }); } }); }); }