I want to pass my array as an input to the radio button field. I have an array of values, when I click a button, it should show me all the values in the array as a radio button.

I tried this, but not able to get any values , only radio button gets created. Can someone help me with this.

/Javascript code/

var curr=0 ;
function addradio(){
   const names =["abi","sam","tom"];
    document.getElementById("my_div").innerHTML=document.getElementById("my_div").innerHTML+
"<br><input type='radio' id='my"+curr+"' name='names[]'"+  
"<input type='radio' id='my"+curr+"' name='names[]'";
  curr=curr+1;
}

Radio buttons let a user select ONLY ONE of a limited number of choices
Use same attribute "name" but array of values use as attribute "value" e.g.

var div = document.createElement('div');
div.setAttribute('id','my_div');
document.body.appendChild(div);

function addradio(){
    const names=["abi","sam","tom"];
    names.forEach(
        function(curr){
            document.getElementById("my_div").innerHTML+='<br/>'
            +'<input type="radio" id="my_'+curr+'" name="names" value="'+curr+'" />'
            +'<label for="my_'+curr+'">'+curr+'</label>';
        }
    );
}

addradio();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.