How to create radio button dynamically using javascript? -
i want display list of data stored in array , against each data want put yes , no radio button. have generated radio button dynamically, radio buttons can select 1 @ time, should like, each data can select either yes or no. please new javascript.
function displaydata() { var data=['apple', 'banana', 'kiwi']; var output=""; var output2=""; var datalist; for(var i=0; i< data.length; i++) { datalist=data[i]; output+= '<input type="checkbox" value='+datalist+' name="box2">' + ' ' + datalist+' '+'<br><br>'; output2+= 'yes:<input type="radio" value="yes" name="box2">'+'no:<input type="radio" value="yes" name="box2">'+'<br><br>'; document.getelementbyid("datalist").innerhtml=output; document.getelementbyid("radiobtn").innerhtml=output2; } }
<html> <body onload="displaydata()"> <div class="row"> <div class="col-sm-4"><div id="datalist"> </div></div> <div class="col-sm-4"><div id="radiobtn"></div></div> </div> </body> </html>
you need assign diffrent name each row of checkbox. please check below code
function displaydata() { var data=['apple', 'banana', 'kiwi']; var output=""; var output2=""; var datalist; for(var i=0; i< data.length; i++) { datalist=data[i]; output+= '<input type="checkbox" value='+datalist+' name="box2'+i+'">' + ' ' + datalist+' '+'<br><br>'; output2+= 'yes:<input type="radio" value="yes" name="box2'+i+'">'+'no:<input type="radio" value="yes" name="box2'+i+'">'+'<br><br>'; document.getelementbyid("datalist").innerhtml=output; document.getelementbyid("radiobtn").innerhtml=output2; } }
Comments
Post a Comment