0
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

17
  • 2
    try console.log(attributeVariables) instead of console.log(thing1 + " , " + thing2), since that's what you are assigning the value to in the first example. You never create variables called thing1 or thing2, so it only makes since that they aren't defined. Commented Aug 4, 2017 at 13:30
  • 1
    Try console.log(attributeVariables.join()) instead Commented Aug 4, 2017 at 13:33
  • 1
    @jonmrich no you are replacing the values in the original array inside the each Commented Aug 4, 2017 at 13:34
  • 1
    No you don't! First those variables don't exist and doing individual variables for 50+ items is crazy. Use an object instead Commented Aug 4, 2017 at 13:36
  • 1
    What is the use case? Can get all this using much simpler approaches depending on what you are trying to do with it all Commented Aug 4, 2017 at 13:38

2 Answers 2

1

As stated at the comments you are not console.log()-ing the right thing. Otherwise your code is almost fine. I think you can use directly val in the each instead attributeVariables[index].

Then you can perform an conditional check because val() of not checked checkboxes returns undefined

var attributeVariables = ['thing1', 'thing2'];

$.each(attributeVariables, function(index, val) {
  attributeVariables[index] = {
    name: val,
    checked: $('input[name=' + val + ']:checked').val() ? true : false
  };
});

console.log(attributeVariables);
console.log('--------------------------------');
console.log('Radio Buttons Test Results');

var radios = ['thing3', 'thing4', 'thing5', 'thing6'];
$.each(radios, function(idx, name) {
  var radioValues = [];
  $.each($('input[name=' + name + ']'), function(i, radio) {
    radioValues.push({index: i, checked: radio.checked});
  });
  
  radios[idx] = {
    name: name,
    radioValues: radioValues
  };
});

console.log(radios);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="checkbox" name="thing1" checked>
<input type="checkbox" name="thing2">
<br/>
<input type="radio" name="thing3">
<input type="radio" name="thing3" checked>
<input type="radio" name="thing3">
<br/>
<input type="radio" name="thing4">
<input type="radio" name="thing4">
<input type="radio" name="thing4" checked>
<br/>
<input type="radio" name="thing5" checked>
<br/>
<input type="radio" name="thing6">

Sign up to request clarification or add additional context in comments.

4 Comments

These are always coming out as true for me regardless of the actual input value. I'm using radio type if that changes things.
Yes, radio is another story, as I commented under your question. But we will work around that
Not sure what you mean by that comment
@jonmrich see the updated code. What I've mean is that if you want radio buttons to work together you need to put them the same name(see my code). Now in the updated code we are taking care for the froups of radio buttons when we do another loop inside the $.each. In the radioValues you will see the index of this radio and if it is checked
1

try this. it might works...

var inputsObject = {};
$("input:checked").each(function() {
    inputsObject[$(this).attr("name")] = $(this).val()
})
console.log(inputsObject)

in inputsObject u will have pairs (name => value) if they exist.

4 Comments

Is there a way to modify this to update variables with the value of each input given that the input name is the same as the variable name?
but look, u have an object inputsObject, inside u have { thing1: on, thing2: off } and then u have easy access from inputsObject to every key of object, which name is equals to input name. O
I know, but I've tried a few things and can't quite get it right. I know I need an each but struggling after that.
with this variant u have access to all of your checked inputs via inputsObject.inputName

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.