0

I have a question on something. So I have written the code below but I have encountered a problem. What I want to happen is after the computer has generated the random number (in the script) I want that number to display right after agility. Sorry this is vague, but I wasn't sure how to explain what I wanted.

<html>
<body>
<h4> Agility </h4>
<script>
function rollAFunction() {
document.getElementById("diceARoll").innerHTML = Math.floor(Math.random() * 20 + 1)
}
</script>
</body>
</html>
3
  • So put the element with id "diceARoll" right after that <h4>. Commented Sep 6, 2015 at 22:26
  • Yep, what @Pointy said. something like <div id="diceARoll"></div> Commented Sep 6, 2015 at 22:31
  • Thanks everybody! I got the problem fixed. Commented Sep 6, 2015 at 22:59

3 Answers 3

1

Just add a span with the id you are accessing right after Agility and call the function to fill it.

  <html>
   <body>
   <h4> Agility <span id="diceARoll"></span></h4>
  <script>
function rollAFunction() {
  document.getElementById("diceARoll").innerHTML = Math.floor(Math.random() * 20 + 1)
  }

 rollAFunction();
 </script>
 </body>

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

1 Comment

You're right, the function does need to be called.. But the question was so vague I wasn't sure if "I want that number to display right after agility" was asking for CSS to style the number directly next to the header!
0

Place the desired element with an id of 'diceARoll' directly after the <h4> element, like so (using a <div> as an example):

<h4> Agility </h4>
<div id="diceARoll"></div>

Comments

0

Is this what you want?

<!Doctype html>
<html>
<body>
<script>

document.write("<h4> Agility " +  Math.floor(Math.random() * 20 + 1) +      "</h4>")
</script>
</body>
</html>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.