0

I would like to be able to use for loop inside of this switch case, currently its writing out single picture. Im bit new to this so i guess its something about the break; prematurely ending the loop.

var katt = ["katt1.jpg","katt.jpg", "katt2.jpg"];

case 'katt':
      for (var i = 0; i < katt.length; i++) {

      document.getElementById("test").innerHTML = `
      <img src="../images/${katt[i]}">
      `;
      }
      break;

Tried this, but still single picture being written out

if(i > katt.length){break;}

Thank you :)

fabelmk1
  • 11
  • 3
  • 2
    that's actually not related to the switch at all. the answer that was given in the closing message won't fix this. the problem here is that you rewrite the same inner HTML of the same element – Saar Davidson Oct 24 '20 at 00:50
  • @DanielA.White This duplicate target seems completely unrelated. This question is about why `for(let i = 0; i < arr.length; ++i){ elem.innerHTML = arr[i]; }` doesn’t keep appending elements, and instead just results in the last one. It’s a duplicate of [For loop through Array only shows last value](https://stackoverflow.com/q/24204825/4642212). – Sebastian Simon Oct 24 '20 at 00:53

0 Answers0