I'm making a multi question survey and I'm trying to save name and value of my form into an object. I am able to put the name and value variables into an alert but I would like to see all the values in one hit, like an array with objects. JS code prints out [object Object] into the alert on the page:
$("button[type='submit']").click(function() {
//get name and value of each question and put into object
$("input[type='radio']:checked").each(function() {
var name = $(this).attr("name");
var value = $(this).attr("value");
var obj = {
name: value
};
alert(obj);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label class="radio">
<input type="radio" name="A1" value="1" required>
<span class="radio__control-indicator"></span>1</label>
<label class="radio">
<input type="radio" name="A1" value="2" required>
<span class="radio__control-indicator"></span>2</label>
<label class="radio">
<input type="radio" name="A2" value="1" required>
<span class="radio__control-indicator"></span>1</label>
<label class="radio">
<input type="radio" name="A2" value="2" required>
<span class="radio__control-indicator"></span>2</label>
<button class="button button--primary right" type="submit">Submit Questionaire</button>