Take right selected value from select options from multiple select tables generated thro for loop : I've tried to get solutions from Google and earlier asked questions but questions and answers relate to only one select having multiple options.  To clarify more, when I generate code thro for loop to create multiple select on screen and then use JavaScript to get selectedIndex or value and use alert to check if correct value is taken , then only value from selected option from first select table generated through for loop is shown repeated. Kindly find the code 

...

<body>

<script>

... JSON code getting g named array throgh JSON.parse method

for (g=0; g<m.length; g++) {

n += "  <select id=\"mySelect\" onchange=\"myFunction()\">";

n += "    <option> - Select Option - </option>";

n += "<option>A</option>";

n += "    <option>B</option>";

n += "    <option>C</option>";

n += "    <option>D</option>";

n += "  </select>";

}

          } // end of for loop

          document.getElementById("demo").innerHTML = n;   

 

.. in between some code

 

function myFunction() {

    var y = document.getElementById("mySelect");

    var j = y.selectedIndex;

    var b = y.options[j].text;

          alert ("Your answer is: " + b);

          } // end of myFunction

 

</script>

<p id="demo"></p>

</body>

</html>

 
when execute this code, e.g. if array g is of length 5, then select-option is shown on screen five times, is perfect, when we select option from first select-options loop alert shows perfect selected option. e.g. if we select C from first select then alert shows C, but when we select options from other four select elements it still shows C ie selected option from first select elements. 

we want our code to show right selected options from different 5 select elements generated throgh above for loop.

request to kindly help, since we have mined a lot for the answer. 

Regards,

Guruprasad 