Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

console.log() works but "textcontent" is not working – JavaScript

I am trying to output a variable in JS to the respective HTML page.
Console.log shows the desired value but for some reason, .textContent (or even .innerText) is not working.

let firstCard = 6
let secondCard = 14
let sum = firstCard + secondCard
let hasBlackJack = false
let isAlive = true
let message = ""
let messageEl = document.getElementById("message-el")
// let sumEl = document.getElementById("sum-el")
let sumEl = document.querySelector("#sum-el") //querySelector - getElement with ID(#) sum-el
console.log(sumEl)

function startGame() {
  sumEl.textcontent = sum
  console.log(sumEl.textcontent)
  if (sum <= 20) {
    message = "Do you want to draw a new card?"
    messageEl.textcontent = message
  } else if (sum === 21) {
    message = "You have got blackjack"
    hasBlackJack = true
  } else {
    message = "You are out of the game"
    isAlive = false
  }

}
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="index.css">
  <title></title>
</head>

<body>
  <h1>BlackJack</h1>
  <p id="message-el">Want to play a round?</p>
  <p>Cards: </p>
  <p id="sum-el">Sum: </p>
  <button onclick="startGame()">Start Game</button>
  <script src="index.js"></script>
</body>

</html>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

it’s very common in javascript to have camel case variable/function names. The issue is that textcontent should be textContent. in your example, it should be:

messageEl.textContent=message

https://developer.mozilla.org/ is a great resource for learning Html and Js.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading