Solution 1 :

As getElementById treats the parameter value as id, you do not need to prefix the id string with the symbol #:


Please Note: You have to prefix the symbol (#) when using querySelector() API:


Problem :

I have a index.html that has a status window for my game, and it displays the number of candy and beads you have collected, updating it in real time as the user collides with the candy and beads.

My entire index.html:
Also, the status-window div is at the bottom. That’s where I display # of candy and beads. The rest of the markup is for the game window.


<title>Mardi Gras Parade!</title>

<!-- JS -->
<script src='scripts/jquery.min.js'></script>
<script src='scripts/page.js'></script>

<!-- CSS -->
<link rel="stylesheet" type="text/css" href="style/style.css">

<!-- HTML -->
  <div class='outer-container'>
    <div class='game-window'>
      <div id="actualGame">
        <div id='player' class='playerObject'>
          <img class='player-avatar' src='img/person.png' height='50px'/> <!--person img: src=''-->
        <div id="paradeRoute">
            <div id="dottedLine"></div>
            <div id="paradeFloats">
              <div id="paradeFloat1" class='paradeFloat'>
                <img src='img/parade_float_1.gif' height='80px'/> <!-- src: -->
              <div id="paradeFloat2" class='paradeFloat'>
                <img src='img/parade_float_2.png' height='80px'/> <!-- Adapted from src: -->

    <div class='status-window' style='text-align: center'>
      <h1 id='score-box'>0</h1>
        <b># of beads collected:</b>
        <span id="beadsCounter">0</span>
        <b># of candy pieces collected:</b>
        <span id="candyCounter">0</span>


Updating the #beadsCounter and #candyCounter with the number of candy and beads collected should be pretty straightforward, as you should be able to do: (where numCandy is an integer I’m incrementing in my logic). The code indeed gets into the if-else block, but errors out when I try to print textContent for both.

let numBeads = 0;
let numCandy = 0;

// functions that check when candy and user collide
function checkCollisions() {
  // First, check for rocket-asteroid checkCollisions
  $('.throwingItem').each( function() {
    let curItem = $(this);  // define a local handle for this rocket
    let curItemID = $(this).attr('id');
    let curItemClass = $(this).attr('class');

    if (isColliding($(this) , player)) {
      // add yellow aura here

      // after 1 second it'll fade from collision
      $(this).fadeTo(1000, 0, function() {

        // update score
        gwhScore.html(parseInt($('#score-box').html()) + SCORE_UNIT);

        // update # of beads collected or candy collected
        if ($(this).attr('class') == 'throwingItem beads yellowaura') {
          document.getElementById('#beadsCounter').textContent = toString(numBeads);
        else {
          document.getElementById('#candyCounter').textContent = toString(numCandy);

However, this doesn’t work for candy or beads, as the console tells me:

page.js:130 Uncaught TypeError: Cannot read property 'textContent' of null
    at HTMLDivElement.<anonymous> (page.js:130)
    at HTMLDivElement.e.complete (jquery.min.js:3)
    at i (jquery.min.js:2)
    at Object.fireWith [as resolveWith] (jquery.min.js:2)
    at i (jquery.min.js:3)
    at Function.r.fx.tick (jquery.min.js:3)
    at ab (jquery.min.js:3)

Also, I have tried this with innerHTML instead of textContent, and it doesn’t work. I’ve tried this JSFiddle and confirms that it should work, but it just doesn’t for me.

The textContent is initially set to 0 for both number of candy and beads, so it is not null.


Comment posted by McFloofenbork

Wow, thank you. Such a silly mistake. I’ll accept in 7 minutes.