Javascript – How to create json by JavaScript for loop


I have array of select tag.

<select id='uniqueID' name="status">
      <option value="1">Present</option>
      <option value="2">Absent</option>

and I want to create a json object having two fields 'uniqueIDofSelect and optionValue' in JavaScript.

I use getElementsByName("status") and I iterate on it.


I need out put like


and so on…

Best Solution

From what I understand of your request, this should work:

//  var status  = document.getElementsByID("uniqueID"); // this works too
var status  = document.getElementsByName("status")[0];
var jsonArr = [];

for (var i = 0; i < status.options.length; i++) {
        id: status.options[i].text,
        optionValue: status.options[i].value