var attributeVariables = ['thing1', 'thing2'];
$.each(attributeVariables, function(index, val) {
attributeVariables[index] = $('input[name='+attributeVariables[index]+']:checked').val();
});
console.log(thing1+" , "+thing2)
In the console, this yields undefined , undefined
However, if I run these, I get the proper value for thing1 and thing2
thing1 = $('input[name=thing1]:checked').val();
thing2 = $('input[name=thing2]:checked').val();
I have a bunch of other variables that a structured the same to get their value. I'm trying to avoid writing the lines above 50 times. I know there's a better way and I thought I had it with my first statement. Can someone tell me why this isn't working?
UPDATE:
All I'm trying to do is update these already existing variables (thing1, thing2) based on the logic in my each...not store everything in an array.
UPDATE 2:
Restated another way...I'm not married to doing this any specific way. Here's what I need: I have about 30 radio selectors, each tied to a different variable. Rather than write this out 30 times for 30 different variables (thing2 = $('input[name=thing2]:checked').val();), I figured there was a shortcut. I'd make a list of all the variables I want updated (based on the radio states) and run a single each
console.log(attributeVariables.join()) insteadeach