This final version, http://jsfiddle.net/dx9omcwd/11/, works the same as the original code, using just 35 lines of code. Included here is the JS code.
var info = {};
var div = [ac, al, am, ap, ba, ce, dsf, es, go, ma, mg, ms, mt, pa, pb, pe, pi, pr, rj, rn, ro, rr, rs, sc, se, sp, to];
var div1 = [ac1, al1, am1, ap1, ba1, ce1, dsf1, es1, go1, ma1, mg1, ms1, mt1, pa1, pb1, pe1, pi1, pr1, rj1, rn1, ro1, rr1, rs1, sc1, se1, sp1, to1];
function esconder() {
$(div).slideUp("slow");
$(div1).hide();
}
$(function () {
var botao = ['aclegenda', 'allegenda', 'amlegenda', 'aplegenda', 'balegenda', 'celegenda', 'dflegenda', 'eslegenda', 'golegenda', 'malegenda', 'mglegenda', 'mslegenda', 'mtlegenda', 'palegenda', 'pblegenda', 'pelegenda', 'pilegenda', 'prlegenda', 'rjlegenda', 'rnlegenda', 'rolegenda', 'rrlegenda', 'rslegenda', 'sclegenda', 'selegenda', 'splegenda', 'tolegenda'];
var botao1 = ['acre', 'alagoas', 'amazonas', 'amapa', 'bahia', 'ceara', 'df', 'espiritosanto', 'goias', 'maranhao', 'minasgerais', 'matogrossosul', 'matogrosso', 'para', 'paraiba', 'pernambuco', 'piaui', 'parana', 'riodejaneiro', 'riograndenorte', 'rondonia', 'roraima', 'riograndesul', 'santacatarina', 'sergipe', 'saopaulo', 'tocantins'];
function prepare() {
var index = 0;
while (index < botao.length) {
info[botao[index]] = info[botao1[index]] = [div[index], div1[index]];
index = index + 1;
}
}
prepare();
esconder();
$('.container').on('click', '.legendalinks.jsvoidcursor, .linksestados1',
function () {
var id = $(this).attr('id'),
curr = info[id];
if ($(curr[0], curr[1]).is(':visible')) {
$(curr[0]).slideUp('slow');
$(curr[1]).hide();
} else {
esconder();
$(curr[0]).slideDown('slow');
$(curr[1]).show();
}
});
});
I'm sure this could be optimized even more, but this answer was meant only to illustrate the use of on to minimize the amount of code that has to be written. In this case, using just one single function with some prepared data greatly reduces the amount of data that's necessary.
Further optimizations would be to use objects instead of just plain arrays to associate the data.