Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

for in loop not populating dropdown list

I’m trying to make a site that shows a select box, and when you select the right character, it shows the pic of that character. In the future, I will fetch a large JSON file with the formatting below, but I’m doing things by little since I’m still learning.

I learned that the for...in loop should get the values from each property, but they’re returning as ‘undefined’. Why is that the case and how can I fix it?

<!DOCTYPE html>
<html>

  <head>
    <title>Add Option From Array</title>
    <meta charset="windows-1252">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
  </head>

  <body>

    <select id="select" onchange="switchImage();"></select>


    <img src="https://abimon.org/dr/busts/aoi/00.png" id="charImg" alt="blank" />

    <script>
      var imageList = new Array();
      
      imageList[0] = new Image();
      imageList[0].src = "https://abimon.org/dr/busts/aoi/00.png";
      imageList[1] = new Image;
      imageList[1].src = "https://abimon.org/dr/busts/akane/00.png";
      imageList[2] = new Image;
      imageList[2].src = "https://ik.imagekit.io/drrp/sprites/angie/00.png";
      
      var select = document.getElementById("select"),
      
      drnames = '{"aoi": "Aoi Asahina", "akane": "Akane Owari", "angie": "Angie Yonaga"}',
      
      arr,
      
      i = 0;

      for (var x in drnames){
        arr[i] = drnames[x];
        i++;
        console.log(arr[i]);
      }
      
        /*arr = ["Aoi Asahina", "Akane Owari", "Angie Yonaga"];*/


      for (var i = 0; i < arr.length; i++) {
        var option = document.createElement("OPTION"),
          txt = document.createTextNode(arr[i]);
        option.appendChild(txt);
        option.setAttribute("value", arr[i]);
        select.appendChild(option);
        
      }

      function switchImage() {
        var index = document.getElementById("select").selectedIndex;
          charImg = document.getElementById("charImg");
        charImg.src = imageList[index].src;
      }

    </script>

  </body>

</html>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I was just picking up my diner – here is the missing explanation:

  • You had assigned a string and not an object to drnames
  • the array arr had not been initialised to be an array.
<!DOCTYPE html>
<html>

  <head>
    <title>Add Option From Array</title>
    <meta charset="windows-1252">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
  </head>

  <body>

    <select id="select" onchange="switchImage();"></select>


    <img src="https://abimon.org/dr/busts/aoi/00.png" id="charImg" alt="blank" />

    <script>
      var imageList = new Array();
      
      imageList[0] = new Image();
      imageList[0].src = "https://abimon.org/dr/busts/aoi/00.png";
      imageList[1] = new Image;
      imageList[1].src = "https://abimon.org/dr/busts/akane/00.png";
      imageList[2] = new Image;
      imageList[2].src = "https://ik.imagekit.io/drrp/sprites/angie/00.png";
      
      var select = document.getElementById("select"),
      
      drnames = {"aoi": "Aoi Asahina", "akane": "Akane Owari", "angie": "Angie Yonaga"},
      
      arr=[],
      
      i = 0;

      for (var x in drnames){
        arr[i] = drnames[x];
        console.log(arr[i]);
        i++;
      }
      
        /*arr = ["Aoi Asahina", "Akane Owari", "Angie Yonaga"];*/


      for (var i = 0; i < arr.length; i++) {
        var option = document.createElement("OPTION"),
          txt = document.createTextNode(arr[i]);
        option.appendChild(txt);
        option.setAttribute("value", arr[i]);
        select.appendChild(option);
        
      }

      function switchImage() {
        var index = document.getElementById("select").selectedIndex;
          charImg = document.getElementById("charImg");
        charImg.src = imageList[index].src;
      }

    </script>

  </body>

</html>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading