0

So I try to run simple function that generates a winner based on number randomly generated. So far so good but when I want not only return the "winner" but return change innerHTML it does not give say ".innerHTML is not a function".

**I tried return also as a return than the document.innerHTML... Also, I try to make it in var and return the var but the same problem always.

function runNum() {
  // randomize of number between 1-6
  var a = Math.floor(Math.random() * 6 + 1);
  var b = Math.floor(Math.random() * 6 + 1);
  console.log(a, b);
  if (a > b) {
    document.querySelector(".winnerclick").innerHTML("winner1");
    console.log("a");
  }
  if (b > a) {
    return document.querySelector(".winnerclick").innerHTML("winner2");
    console.log("b");
  }
  if (a === b) {
    document.querySelector(".winnerclick").innerTEXT("Draw - no winner");
  }
}
<h1 class="winnerclick">
  Click on the button to play!
</h1>
<button onclick="runNum()">
Click to start
</button>

I try found answers but nothing solved this problem. Why it's not returned?

2
  • 1
    .innerHTML is not a function. You assign a value to it. Commented May 20, 2020 at 18:08
  • 1
    Have you taken a look at the innerHTML documentation? It's defined as a property, not a function. Commented May 20, 2020 at 18:10

2 Answers 2

1

Actually, the error you are facing is correct since the innerHTML is not a function so you can't invoke it with parentheses either. You just can set its value by assigning the values to it.

So your final code should be something like this:

function runNum() {
  // randomize of number between 1-6
  var a = Math.floor(Math.random() * 6 + 1);
  var b = Math.floor(Math.random() * 6 + 1);
  console.log(a, b);
  if (a > b) {
    document.querySelector(".winnerclick").innerHTML = "winner1";
    console.log("a");
  }
  if (b > a) {
    return document.querySelector(".winnerclick").innerHTML = "winner2";
    console.log("b");
  }
  if (a === b) {
    document.querySelector(".winnerclick").innerTEXT = "Draw - no winner";
  }
}
<h1 class="winnerclick">
  Click on the button to play!
</h1>
<button onclick="runNum()">
Click to start
</button>

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

1 Comment

OMG how do i missed it? i spend at least 2 hours to figure what going on it's all looks great but = instead of (). Thank you so much guys!
1

Pointy is correct, but I feel like their answer may need to be a little clarified. When you want to change the value of .innerHTML, you don't use .innerHTML("new value"); you use .innerHTML = "new value";

1 Comment

Thank you so much i don't get how i missed it after two hours. Just the syntax and it's after i saw documentations and other questions. Thank you!

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.