0

I need a random integer in a table in HTML, and have the correct code but it doesn't work for some reason! Here is my code so far:

document.getElementById(Example).innerHTML = Math.floor(Math.random() * 13)  + 1;

My error message: https://i.sstatic.net/OhR8X.jpg My code: https://i.sstatic.net/ji2lL.jpg

16
  • It needs to be 'Example' or "Example", not just Example. Commented Aug 3, 2018 at 8:27
  • @CodeF0x this still has not fixed my problem Commented Aug 3, 2018 at 8:28
  • Does getElementById() find the element? Any errors in your browser's dev console? (When you press F12 and head to "Console") Commented Aug 3, 2018 at 8:30
  • Can you post your html ? Commented Aug 3, 2018 at 8:32
  • 1
    yes look at @tiagos answer Commented Aug 3, 2018 at 8:33

8 Answers 8

3

If you assign your table cell a valid ID, your code should work:

document.getElementById("example").innerHTML = Math.floor(Math.random() * 13)  + 1;
<table>
  <tr>
    <td id="example"></td>
  </tr>
</table>

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

Comments

1

you must have something like this.

As the CodeF0x spoke, you have to have 'example' or "example" in the id when you fetch the element

document.getElementById('example').innerHTML = Math.floor(Math.random() * 13) + 1;
<div id="example"></div>

Comments

1

if your html tag is <input type='text' id='example'/>

document.getElementById("example").value = Math.floor(Math.random() * 13)  + 1;

if your html tag is <td id='example'></td>

document.getElementById("example").innerHTML = Math.floor(Math.random() * 13)  + 1;

document.getElementById("example").value = Math.floor(Math.random() * 13)  + 1;

document.getElementById("exampleTd").innerHTML = Math.floor(Math.random() * 13)  + 1;
<input type='text' id='example'/>

<table>
  <tr><td id='exampleTd'></td></tr>
</table>

Comments

0

Here's an example using the code you have to generate a random number.

var cell = document.getElementById('random');

cell.innerHTML = Math.floor(Math.random() * 13) + 1
<table>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td id="random">3</td>
</tr>
</table>

Comments

0

The ID should be quoted if you are using it directly or you can put it in a variable and use it that way:

document.getElementById('Example').innerHTML = Math.floor(Math.random() * 13) + 1;

var a='answers-header';
document.getElementById(a).innerHTML=Math.floor(Math.random() * 13) + 2;

Comments

0

Here's an example with list

<body>
 <ul id="myList">

 </ul>

 <button id="btn" onclick="addRandomNumber()">Add number</button>
</body>

Script

   function addRandomNumber(){
    var randomNum = Math.floor(Math.random() * 13)  + 1;

    var node = document.createElement("LI");
    var textnode = document.createTextNode(randomNum);
    node.appendChild(textnode);
    document.getElementById("myList").appendChild(node);

  }

Comments

0

typeError :Cannot set property "innerHtml" of null

it can not find your element "Example" Because you write script before Html tag

Comments

0

document.getElementById("pointsA1").innerHTML = Math.floor(Math.random() * 13)  + 1;
<table>
  <tr>
    <th>Country</th>
    <th>Points</th>
  </tr>
  <tr>
    <td>England</td>
    <td id="pointsA1"></td>
  </tr>
</table>

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.