For loop through Array only shows last value

后端 未结 1 818
情深已故
情深已故 2020-12-18 09:25

I\'m trying to loop through an Array which then uses innerHTML to create a new

  • element for every entry in the array. Somehow my code is only showing the last value from
  • 相关标签:
    1条回答
    • 2020-12-18 09:52

      Change your for loop:

      for (i = 0; i <= (n-1); i++) {
          var list = names[i];
          var myList = document.getElementById("list");
          myList.innerHTML += "<li class='list-group-item' id='listItem'>"+ list + "</li>" + "<br />";
      }
      

      Use += instead of =. Other than that, your code looks fine.

      0 讨论(0)
    提交回复
    热议问题