How to get the values of all radio button as array ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How to get the values of all radio button as array ?

function afterButtonClick() { var values = []; for( j = 1; j < 25; j++ ) {      var ele = document.getElementsByName('radio'+j);                      for(i = 0; i < 2; i++) {                  if(ele[i].checked) {                 var result  = ele[i].value;              }      values.push(result);  }     }       alert(values); } Output values= yes,yes,No,yes,no like this

28th May 2020, 1:26 PM
Prince Chaurasia
Prince Chaurasia - avatar
5 Answers
+ 3
You can use the for/of loop to iterate over the HTML collection of radio buttons. This way you don't need to worry about the length or index of the collection. And within the loop push the element value, if it is checked, to the array. https://code.sololearn.com/WoUrFl5expSO/#html
28th May 2020, 4:16 PM
ODLNT
ODLNT - avatar
28th May 2020, 2:20 PM
Prince Chaurasia
Prince Chaurasia - avatar
+ 1
Deep_Blue Show only 1st value
28th May 2020, 3:00 PM
Prince Chaurasia
Prince Chaurasia - avatar
+ 1
Want to get all button value after submit
28th May 2020, 3:02 PM
Prince Chaurasia
Prince Chaurasia - avatar