Click here to Skip to main content
15,940,550 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am working in a project for which i require a hidden field value to store array of values in it using jquery or javascript. Because i have got the values and stored in local javascript variable using function. That values i required to store it in hidden field to access that values in server side. Actually what i needed is radio button checked values.

What I have tried:

for (let i = 1; i <= count; i++) {
    let values = getRadioVal(document.getElementById("form1"), 'name' + i);

// With this function i am getting values that radio button checked
function getRadioVal(form, name) {
    let val;
    // get list of radio buttons with specified name
    let radios = form.elements[name];
    // loop through list of radio buttons
    for (var i = 0, len = radios.length; i < len; i++) {
        if (radios[i].checked) { // radio checked?
            val = radios[i].value; // if so, hold its value in val
            break; // and break out of for loop
    return val; // return value of checked radio or undefined if none checked

To store the values in hidden field i have tried this but resulting [Object Object] value
let array = [];
for (array in values)

I want values to be stored in hidden field how can i achieve this please anybody help me
Updated 21-Dec-18 4:24am

1 solution

values stores the selected value from the last radio button group you check. You're then iterating over the properties and methods of the string type[^], which isn't what you want.

You shouldn't need a separate field - the value of the selected radio buttons will be posted back to the server when you submit your form. But if you still want to do it, you'll need to push the selected values into an array:
let array = [];
let theForm = document.getElementById("form1");

for (let i = 1; i <= count; i++) {
    array.push(getRadioVal(theForm, 'name' + i));

document.getElementById('hdnValue').value = JSON.stringify(array);
Share this answer
Member 8583441 21-Dec-18 23:22pm    
getting error in console.log i is not defined and the values are not pushing into array. The array values displaying as object object till the count value completes
Member 8583441 22-Dec-18 4:29am    
in javascript hidden field values are storing as expected but when button click event fires it changes to last value can anybody tell me for this

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900