0

I want to output 2 different random array objects. This is what I got so far. But It only shows 1 output instead of 2. How is this possible? I want to output 2 different random array objects. This is what I got so far. But It only shows 1 output instead of 2. How is this possible?

<!DOCTYPE html>
<html>
<head>

</head>

<body>
<script>


  
  var myArray = [
  "Apples",
  "Bananas",
  "Pears"
];

    
var randomItem1 = myArray[Math.floor(Math.random()*myArray.length)];
var randomItem2 = myArray[Math.floor(Math.random()*myArray.length)];


document.getElementById("randomItem1").innerHTML=randomItem1;
document.getElementById("randomItem2").innerHTML=randomItem2;

  

</script>


<div id = "randomItem1"> </div>
<div id = "randomItem2"> </div>

</body>

</html>

1
  • 1
    As the other answerer got it right now, and were first to answer, may I suggest you change mine to an upvote and accept the other? Commented Jul 7, 2018 at 22:08

2 Answers 2

1

Your code is almost correct, the only problem is you are overwriting to your document body, instead of two different divs.

You are also executing javascript before html, so it searches for elements that are not loaded. Putting javascript after the html solves that problem.

Do it like this:

<!DOCTYPE html>
<html>
<head>

</head>

<body>



<div id = "randomItem1"> </div>
<div id = "randomItem2"> </div>

</body>
<script>



    var myArray = [
    "Apples",
    "Bananas",
    "Pears"
  ];


  var randomItem1 = myArray[Math.floor(Math.random()*myArray.length)];
  var randomItem2 = myArray[Math.floor(Math.random()*myArray.length)];


  document.getElementById("randomItem1").innerHTML=randomItem1;
  document.getElementById("randomItem2").innerHTML=randomItem2;



</script>
</html>

For testing, you can add console.log(randomItem1) and console.log(randomItem2) between the lines.

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

7 Comments

Thanks for your respond, I got this error Uncaught TypeError: Cannot set property 'innerHTML' of null.
This is strange, I tried it now, and it works, are you sure that you are not making a typo with ids.
I've edited my question's snippets you can also see the error in there
Okey, Fixed it. Always put your <script> at the end of the </body> . So either as last block before the </body> or between </body> and </html>. Your scripts should execute after html is loaded, so that your document.getElementById can actually find the elements :D
@atayenel Add that to your answer, where it belongs.
|
1

There are 2 things that goes one, and wrong:

  1. the document.body.innerHTML = randomItem1; will overwrite the existing elements

  2. when update to e.g. document.getElementById("randomItem1").innerHTML = randomItem1;, the <script> block needs to be positioned after the elements, or else they won't be found not being parse and loaded in the DOM.

<!DOCTYPE html>
<html>
<head>

</head>

<body>

<div id = "randomItem1"> </div>
<div id = "randomItem2"> </div>

<script>
  var myArray = [
    "Apples",
    "Bananas",
    "Pears"
  ];

  var randomItem1 = myArray[Math.floor(Math.random()*myArray.length)];
  var randomItem2 = myArray[Math.floor(Math.random()*myArray.length)];

  document.getElementById("randomItem1").innerHTML = randomItem1;
  document.getElementById("randomItem2").innerHTML = randomItem2;
</script>

</body>

</html>


An alternative is to use an event listener that will fire when the DOM is ready.

<!DOCTYPE html>
<html>
<head>

</head>

<body>

<script>
  var myArray = [
    "Apples",
    "Bananas",
    "Pears"
  ];

  var randomItem1 = myArray[Math.floor(Math.random()*myArray.length)];
  var randomItem2 = myArray[Math.floor(Math.random()*myArray.length)];

  document.addEventListener("DOMContentLoaded", function(event) {

    document.getElementById("randomItem1").innerHTML = randomItem1;
    document.getElementById("randomItem2").innerHTML = randomItem2;

  });
</script>

<div id = "randomItem1"> </div>
<div id = "randomItem2"> </div>

</body>

</html>

Comments

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.