0

code.js(js file)

let winter = ["applle","orange","grape"];
console.log(winter);
document.getElementById("fruits").innerHTML = winter;
<h1 id="fruits">fruit stall</h1>

Why it's not working for me, can anyone help on this please...

I want array elements to be as output instead of "fruit stall"

derpirscher
  • 14,418
  • 3
  • 18
  • 35
Learner
  • 1
  • 1
  • 2
    Works as expected. Are you sure your JS file is in the correct location where it can be found? – derpirscher Aug 20 '23 at 12:41
  • 1
    Works for me. Firefox v113.0.2, Ubuntu v18.04. – Marc Aug 20 '23 at 12:41
  • when you try to access html element you should make sure the element is rendering. so to solve this problem you should add your code on `windows.onload` event ``` window.onload = function() { let winter = ["applle","orange","grape"]; console.log(winter); document.getElementById("fruits").innerHTML = winter; }; ``` – Ayah K Alrifai Aug 20 '23 at 12:46

1 Answers1

-3

Your code works perfectly:

let winter = ["applle", "orange", "grape"];
console.log(winter);
document.getElementById("fruits").innerHTML = winter;
<h1 id="fruits"> fruit stall</h1>
Guerric P
  • 30,447
  • 6
  • 48
  • 86
  • So strange. It's working now. The file location was good. I did nothing and it started working now. Before I got this error: error code.js:326 Uncaught TypeError: Cannot set properties of null (setting 'innerHTML') at code.js:326:46 – Learner Aug 20 '23 at 13:00