Here's what i'm trying to accomplish: In the following mappings2
object, the keys are the name property of some grouped radio buttons and the value is a list which first string is a class that it's attached to a radio (might be more than one) of that group, let's call this radio button special-radio, and the second string is a class whose elements must be enabled when an special-radio is clicked, the code below is supposed to toggle between change events in a group of radios, letting execute some code when special-radio is clicked and a diferent code when other non-special radios from that group are clicked.
var mappings2 = {
// Group's name special-radio's class other radios
'DependientesEconomicos': ['hijos-dependientes', 'cuantos-hijos'],
'Preparatoria': ['otra-prepa', 'nombre-prepa'],
'ReproboAlgunaMateria': ['reprobo', 'causas-reprobo']
//'CausasReprobo': ['otra-causa-reprobo', 'otra-causa-reprobo-text']
};
(function (maps) {
for (grupo in maps) {
$('input[name="' + grupo + '"]').change(function (e) {
if ($(this).attr('class') === maps[grupo][0]) {
console.log(grupo + ' ' + $(this).attr('class') + ' true');
$('.' + maps[grupo][1]).attr('disabled', false);
} else {
$('.' + maps[grupo][1]).attr('disabled', true);
console.log(grupo + ' ' + $(this).attr('class'));
$('.' + maps[grupo][1]).val('');
}
});
}
})(mappings2);
The code works fine for one entry for mappings2
but when you add more only the last entry works, and if you click one of the radios from previous groups the log shows the correct class, but grupo
is not the group it belongs to, it shows the last entry's group, so that's the source of all the bugs, my question is why.
jquery 1.5.1
Thanks.