So this is the HTML code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<script src="main.js"></script>
<title>Pig Game Simple Version</title>
</head>
<body>
<div class="wrapper clearfix">
<div class="player-0-panel active">
<div class="player-name" id="name-0">Player 1</div>
<div class="player-score" id="score-0">43</div>
<div class="player-current-box">
<div class="player-current-label">Current</div>
<div class="player-current-score" id="current-0">11</div>
</div>
</div>
<div class="player-1-panel">
<div class="player-name" id="name-1">Player 2</div>
<div class="player-score" id="score-1">72</div>
<div class="player-current-box">
<div class="player-current-label">Current</div>
<div class="player-current-score" id="current-1">0</div>
</div>
</div>
<button class="btn-new"></i>New game</button>
<button class="btn-roll"></i>Roll dice</button>
<button class="btn-hold"></i>Hold</button>
<div class="dice">5</div>
</body>
</html>
And this is the code in JS:
var scores=[0,0];
var roundScore=0;
var activePlayer=0;
var maximumPoints=100;
var dice=Math.floor(Math.random() * 6) +1;
var score0=document.getElementById("current-0");
score0.innerHTML+="";
score0.innerHTML=dice.toString();
The thing is that with the debugger it tells me that it is unable to take the label whose id is "current-0", it tells me that it is null, and of course it tells me that "Cannot read property InnerHTML of null"
. And honestly, I don't see the error anywhere.
Thank you very much.
You must put the javascript code inside window.onload
I assume you are calling the JS code before the DOM loads.