0

I am trying to use innerHTML with arrays and it does not work, but if i use document.write my function works properly and I get the following: product 1 price 1 product 2 price 2 product 3 price 3 product 4 price 4 product 5 price 5 Any help will be appreciated. Following is the code:

var items = [
  ["product 1", "price 1"],
  ["product 2", "price 2"],
  ["product 3", "price 3"],
  ["product 4", "price 4"],
  ["product 5", "price 5"]
];


function testButton() {

  var j = 0;
  for (var i = 0; i < items.length; i++) {
    for (var j = 0; j < 2; j++) {
      document.getElementById("buttons").innerHTML = items[i][j] + "<br>";
      /*document.write(items[i][j] + "<br>");*/
    }

  }

}
#buttons {
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100vw;
  height: 100vh;
  text-align: center;
  text-transform: uppercase;
  padding: 10px;
  font-size: 2em;
  border: solid;
}
<button id="buttons" onclick="testButton()">Next</button>

2
  • 2
    seems to work, you overwrite button with price 1, then price 2 etc ... you'll only ever see "price 5" though Commented Nov 28, 2017 at 23:56
  • 1
    @JaromandaX is right, since the browser works too fast to recognize, you couldn't even know what happen in the middle of its work. So technically, the value has been changed, but you could only see the last one when the browser stopped to work. Commented Nov 29, 2017 at 0:05

2 Answers 2

3

You can build up the html in the loop and append it to a variable. Then after the loop ends, set the HTML for the button element.

var items = [
  ["product 1", "price 1"],
  ["product 2", "price 2"],
  ["product 3", "price 3"],
  ["product 4", "price 4"],
  ["product 5", "price 5"]
];

function testButton() {
  var html = '';
  var j = 0;
  for (var i = 0; i < items.length; i++) {
    for (var j = 0; j < 2; j++) {
      html += items[i][j] + "<br>";
      /*document.write(items[i][j] + "<br>");*/
    }
  }
  document.getElementById("buttons").innerHTML = html;
}
#buttons {
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100vw;
  height: 100vh;
  text-align: center;
  text-transform: uppercase;
  padding: 10px;
  font-size: 2em;
  border: solid;
}
<button id="buttons" onclick="testButton()">Next</button>

Sign up to request clarification or add additional context in comments.

Comments

0

The code you've written replaces the contents of the button on each iteration of the for loop.

document.getElementById("buttons").innerHTML = items[i][j] + "<br>";

I think you want to append the contents, which I'm pretty sure is what successive calls to document.write does.

document.getElementById("buttons").innerHTML += items[i][j] + "<br>";

You may need to clear this before starting the for loop depending on your requirements.

2 Comments

Thank you very much. Just a + sign helped a lot.
I'm glad to hear that. If you consider this answer helpful, don't forget to upvote/accept it :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.