Can't update textContent of (that is assigned to a variable) from within the callback












0















I have a simple HTML page



enter image description here



When I press on buttons "Player 1" and "Player 2", I want changes to reflect on the page. In console scores go up, everything's fine, but why page doesn't update the changed score? (let p1score and let p2score)



Here's my code, thanks !



const p1button = document
.querySelector('#p1')
.addEventListener('click', scoreUp);
const p2button = document
.querySelector('#p2')
.addEventListener('click', scoreUp);

let p2score = document.querySelector('#p2score').textContent;
let p1score = document.querySelector('#p1score').textContent;

function scoreUp(e) {
if (e.target.textContent === 'Player 1') {
p1score++;
console.log(p1score);
} else {
p2score++;
console.log(p2score);
}
}









share|improve this question























  • You need to include your code instead of image, please add your full code.

    – Just code
    Nov 22 '18 at 5:03






  • 2





    p2score and p1score are just references. Adding them won't reflect on page. You need to assign it back. It's not a two-way binding thing.

    – wannadream
    Nov 22 '18 at 5:04


















0















I have a simple HTML page



enter image description here



When I press on buttons "Player 1" and "Player 2", I want changes to reflect on the page. In console scores go up, everything's fine, but why page doesn't update the changed score? (let p1score and let p2score)



Here's my code, thanks !



const p1button = document
.querySelector('#p1')
.addEventListener('click', scoreUp);
const p2button = document
.querySelector('#p2')
.addEventListener('click', scoreUp);

let p2score = document.querySelector('#p2score').textContent;
let p1score = document.querySelector('#p1score').textContent;

function scoreUp(e) {
if (e.target.textContent === 'Player 1') {
p1score++;
console.log(p1score);
} else {
p2score++;
console.log(p2score);
}
}









share|improve this question























  • You need to include your code instead of image, please add your full code.

    – Just code
    Nov 22 '18 at 5:03






  • 2





    p2score and p1score are just references. Adding them won't reflect on page. You need to assign it back. It's not a two-way binding thing.

    – wannadream
    Nov 22 '18 at 5:04
















0












0








0








I have a simple HTML page



enter image description here



When I press on buttons "Player 1" and "Player 2", I want changes to reflect on the page. In console scores go up, everything's fine, but why page doesn't update the changed score? (let p1score and let p2score)



Here's my code, thanks !



const p1button = document
.querySelector('#p1')
.addEventListener('click', scoreUp);
const p2button = document
.querySelector('#p2')
.addEventListener('click', scoreUp);

let p2score = document.querySelector('#p2score').textContent;
let p1score = document.querySelector('#p1score').textContent;

function scoreUp(e) {
if (e.target.textContent === 'Player 1') {
p1score++;
console.log(p1score);
} else {
p2score++;
console.log(p2score);
}
}









share|improve this question














I have a simple HTML page



enter image description here



When I press on buttons "Player 1" and "Player 2", I want changes to reflect on the page. In console scores go up, everything's fine, but why page doesn't update the changed score? (let p1score and let p2score)



Here's my code, thanks !



const p1button = document
.querySelector('#p1')
.addEventListener('click', scoreUp);
const p2button = document
.querySelector('#p2')
.addEventListener('click', scoreUp);

let p2score = document.querySelector('#p2score').textContent;
let p1score = document.querySelector('#p1score').textContent;

function scoreUp(e) {
if (e.target.textContent === 'Player 1') {
p1score++;
console.log(p1score);
} else {
p2score++;
console.log(p2score);
}
}






javascript dom






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 4:58









max23701max23701

414




414













  • You need to include your code instead of image, please add your full code.

    – Just code
    Nov 22 '18 at 5:03






  • 2





    p2score and p1score are just references. Adding them won't reflect on page. You need to assign it back. It's not a two-way binding thing.

    – wannadream
    Nov 22 '18 at 5:04





















  • You need to include your code instead of image, please add your full code.

    – Just code
    Nov 22 '18 at 5:03






  • 2





    p2score and p1score are just references. Adding them won't reflect on page. You need to assign it back. It's not a two-way binding thing.

    – wannadream
    Nov 22 '18 at 5:04



















You need to include your code instead of image, please add your full code.

– Just code
Nov 22 '18 at 5:03





You need to include your code instead of image, please add your full code.

– Just code
Nov 22 '18 at 5:03




2




2





p2score and p1score are just references. Adding them won't reflect on page. You need to assign it back. It's not a two-way binding thing.

– wannadream
Nov 22 '18 at 5:04







p2score and p1score are just references. Adding them won't reflect on page. You need to assign it back. It's not a two-way binding thing.

– wannadream
Nov 22 '18 at 5:04














3 Answers
3






active

oldest

votes


















2














When you do let p1score = document.querySelector('#p1score').textContent; you are effectively saving the value of the text content, not a reference to that value. Thus, p1score and p2score hold their own unique copies of the values (integers) of the text content.



So, when you increment p1score and p2score you are only incrementing the values in held by these variables. Thus, once you've incremented your value, you should set the text content to be the incremented value by doing



pScoreOneElem.textContent = p1score;



pScoreTwoElem.textContent = p2score;



This will effectively update the text content by changing its value to reflect the changes to p1score and p2score:




const p1button = document
.querySelector('#p1')
.addEventListener('click', scoreUp);
const p2button = document
.querySelector('#p2')
.addEventListener('click', scoreUp);

let pScoreOneElem = document.querySelector('#p1score');
let pScoreTwoElem = document.querySelector('#p2score');

let p2score = pScoreTwoElem.textContent;
let p1score = pScoreOneElem.textContent;

function scoreUp(e) {
if (e.target.textContent === 'Player 1') {
p1score++;
pScoreOneElem.textContent = p1score;
console.log(p1score);
} else {
p2score++;
pScoreTwoElem.textContent = p2score;
console.log(p2score);
}
}

<button id="p1">Player 1</button>
<button id="p2">Player 2</button>

<br />
<div class="player-scores">
Player 1 Score: <span id="p1score">0</span>
<br />
Player 2 Score: <span id="p2score">0</span>
</div>








share|improve this answer


























  • That's a lot of repeated DOM querying. I'd just save references to the two <span> elements

    – Phil
    Nov 22 '18 at 5:08











  • @Phil You're right, updated ;)

    – Nick Parsons
    Nov 22 '18 at 5:10



















1














let p2score = document.querySelector('#p2score').textContent only assigns the value of textContent to the score variable. That value is no longer connected to the element text content property. You must reference the element instead



const p1button = document
.querySelector('#p1')
.addEventListener('click', scoreUp);
const p2button = document
.querySelector('#p2')
.addEventListener('click', scoreUp);

let p1 = document.querySelector('#p1score');
let p2 = document.querySelector('#p2score');
let p1score = p1.textContent;
let p2score = p2.textContent;

function scoreUp(e) {
if (e.target.textContent === 'Player 1') {
p1.textContent = p1score++;
console.log(p1.textContent);
} else {
p2.textContent = p2score++;
console.log(p2.textContent);
}
}





share|improve this answer































    0














    You need to set scores to p1score and p2score elements like so :



    if (e.target.textContent === 'Player 1') {
    p1score++;
    document.querySelector('#p1score').textContent = p1score;
    console.log(p1score);
    } else {
    p2score++;
    document.querySelector('#p2score').textContent = p2score;
    console.log(p2score);
    }





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424150%2fcant-update-textcontent-of-span-that-is-assigned-to-a-variable-from-within%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      When you do let p1score = document.querySelector('#p1score').textContent; you are effectively saving the value of the text content, not a reference to that value. Thus, p1score and p2score hold their own unique copies of the values (integers) of the text content.



      So, when you increment p1score and p2score you are only incrementing the values in held by these variables. Thus, once you've incremented your value, you should set the text content to be the incremented value by doing



      pScoreOneElem.textContent = p1score;



      pScoreTwoElem.textContent = p2score;



      This will effectively update the text content by changing its value to reflect the changes to p1score and p2score:




      const p1button = document
      .querySelector('#p1')
      .addEventListener('click', scoreUp);
      const p2button = document
      .querySelector('#p2')
      .addEventListener('click', scoreUp);

      let pScoreOneElem = document.querySelector('#p1score');
      let pScoreTwoElem = document.querySelector('#p2score');

      let p2score = pScoreTwoElem.textContent;
      let p1score = pScoreOneElem.textContent;

      function scoreUp(e) {
      if (e.target.textContent === 'Player 1') {
      p1score++;
      pScoreOneElem.textContent = p1score;
      console.log(p1score);
      } else {
      p2score++;
      pScoreTwoElem.textContent = p2score;
      console.log(p2score);
      }
      }

      <button id="p1">Player 1</button>
      <button id="p2">Player 2</button>

      <br />
      <div class="player-scores">
      Player 1 Score: <span id="p1score">0</span>
      <br />
      Player 2 Score: <span id="p2score">0</span>
      </div>








      share|improve this answer


























      • That's a lot of repeated DOM querying. I'd just save references to the two <span> elements

        – Phil
        Nov 22 '18 at 5:08











      • @Phil You're right, updated ;)

        – Nick Parsons
        Nov 22 '18 at 5:10
















      2














      When you do let p1score = document.querySelector('#p1score').textContent; you are effectively saving the value of the text content, not a reference to that value. Thus, p1score and p2score hold their own unique copies of the values (integers) of the text content.



      So, when you increment p1score and p2score you are only incrementing the values in held by these variables. Thus, once you've incremented your value, you should set the text content to be the incremented value by doing



      pScoreOneElem.textContent = p1score;



      pScoreTwoElem.textContent = p2score;



      This will effectively update the text content by changing its value to reflect the changes to p1score and p2score:




      const p1button = document
      .querySelector('#p1')
      .addEventListener('click', scoreUp);
      const p2button = document
      .querySelector('#p2')
      .addEventListener('click', scoreUp);

      let pScoreOneElem = document.querySelector('#p1score');
      let pScoreTwoElem = document.querySelector('#p2score');

      let p2score = pScoreTwoElem.textContent;
      let p1score = pScoreOneElem.textContent;

      function scoreUp(e) {
      if (e.target.textContent === 'Player 1') {
      p1score++;
      pScoreOneElem.textContent = p1score;
      console.log(p1score);
      } else {
      p2score++;
      pScoreTwoElem.textContent = p2score;
      console.log(p2score);
      }
      }

      <button id="p1">Player 1</button>
      <button id="p2">Player 2</button>

      <br />
      <div class="player-scores">
      Player 1 Score: <span id="p1score">0</span>
      <br />
      Player 2 Score: <span id="p2score">0</span>
      </div>








      share|improve this answer


























      • That's a lot of repeated DOM querying. I'd just save references to the two <span> elements

        – Phil
        Nov 22 '18 at 5:08











      • @Phil You're right, updated ;)

        – Nick Parsons
        Nov 22 '18 at 5:10














      2












      2








      2







      When you do let p1score = document.querySelector('#p1score').textContent; you are effectively saving the value of the text content, not a reference to that value. Thus, p1score and p2score hold their own unique copies of the values (integers) of the text content.



      So, when you increment p1score and p2score you are only incrementing the values in held by these variables. Thus, once you've incremented your value, you should set the text content to be the incremented value by doing



      pScoreOneElem.textContent = p1score;



      pScoreTwoElem.textContent = p2score;



      This will effectively update the text content by changing its value to reflect the changes to p1score and p2score:




      const p1button = document
      .querySelector('#p1')
      .addEventListener('click', scoreUp);
      const p2button = document
      .querySelector('#p2')
      .addEventListener('click', scoreUp);

      let pScoreOneElem = document.querySelector('#p1score');
      let pScoreTwoElem = document.querySelector('#p2score');

      let p2score = pScoreTwoElem.textContent;
      let p1score = pScoreOneElem.textContent;

      function scoreUp(e) {
      if (e.target.textContent === 'Player 1') {
      p1score++;
      pScoreOneElem.textContent = p1score;
      console.log(p1score);
      } else {
      p2score++;
      pScoreTwoElem.textContent = p2score;
      console.log(p2score);
      }
      }

      <button id="p1">Player 1</button>
      <button id="p2">Player 2</button>

      <br />
      <div class="player-scores">
      Player 1 Score: <span id="p1score">0</span>
      <br />
      Player 2 Score: <span id="p2score">0</span>
      </div>








      share|improve this answer















      When you do let p1score = document.querySelector('#p1score').textContent; you are effectively saving the value of the text content, not a reference to that value. Thus, p1score and p2score hold their own unique copies of the values (integers) of the text content.



      So, when you increment p1score and p2score you are only incrementing the values in held by these variables. Thus, once you've incremented your value, you should set the text content to be the incremented value by doing



      pScoreOneElem.textContent = p1score;



      pScoreTwoElem.textContent = p2score;



      This will effectively update the text content by changing its value to reflect the changes to p1score and p2score:




      const p1button = document
      .querySelector('#p1')
      .addEventListener('click', scoreUp);
      const p2button = document
      .querySelector('#p2')
      .addEventListener('click', scoreUp);

      let pScoreOneElem = document.querySelector('#p1score');
      let pScoreTwoElem = document.querySelector('#p2score');

      let p2score = pScoreTwoElem.textContent;
      let p1score = pScoreOneElem.textContent;

      function scoreUp(e) {
      if (e.target.textContent === 'Player 1') {
      p1score++;
      pScoreOneElem.textContent = p1score;
      console.log(p1score);
      } else {
      p2score++;
      pScoreTwoElem.textContent = p2score;
      console.log(p2score);
      }
      }

      <button id="p1">Player 1</button>
      <button id="p2">Player 2</button>

      <br />
      <div class="player-scores">
      Player 1 Score: <span id="p1score">0</span>
      <br />
      Player 2 Score: <span id="p2score">0</span>
      </div>








      const p1button = document
      .querySelector('#p1')
      .addEventListener('click', scoreUp);
      const p2button = document
      .querySelector('#p2')
      .addEventListener('click', scoreUp);

      let pScoreOneElem = document.querySelector('#p1score');
      let pScoreTwoElem = document.querySelector('#p2score');

      let p2score = pScoreTwoElem.textContent;
      let p1score = pScoreOneElem.textContent;

      function scoreUp(e) {
      if (e.target.textContent === 'Player 1') {
      p1score++;
      pScoreOneElem.textContent = p1score;
      console.log(p1score);
      } else {
      p2score++;
      pScoreTwoElem.textContent = p2score;
      console.log(p2score);
      }
      }

      <button id="p1">Player 1</button>
      <button id="p2">Player 2</button>

      <br />
      <div class="player-scores">
      Player 1 Score: <span id="p1score">0</span>
      <br />
      Player 2 Score: <span id="p2score">0</span>
      </div>





      const p1button = document
      .querySelector('#p1')
      .addEventListener('click', scoreUp);
      const p2button = document
      .querySelector('#p2')
      .addEventListener('click', scoreUp);

      let pScoreOneElem = document.querySelector('#p1score');
      let pScoreTwoElem = document.querySelector('#p2score');

      let p2score = pScoreTwoElem.textContent;
      let p1score = pScoreOneElem.textContent;

      function scoreUp(e) {
      if (e.target.textContent === 'Player 1') {
      p1score++;
      pScoreOneElem.textContent = p1score;
      console.log(p1score);
      } else {
      p2score++;
      pScoreTwoElem.textContent = p2score;
      console.log(p2score);
      }
      }

      <button id="p1">Player 1</button>
      <button id="p2">Player 2</button>

      <br />
      <div class="player-scores">
      Player 1 Score: <span id="p1score">0</span>
      <br />
      Player 2 Score: <span id="p2score">0</span>
      </div>






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 22 '18 at 5:55

























      answered Nov 22 '18 at 5:07









      Nick ParsonsNick Parsons

      8,5952824




      8,5952824













      • That's a lot of repeated DOM querying. I'd just save references to the two <span> elements

        – Phil
        Nov 22 '18 at 5:08











      • @Phil You're right, updated ;)

        – Nick Parsons
        Nov 22 '18 at 5:10



















      • That's a lot of repeated DOM querying. I'd just save references to the two <span> elements

        – Phil
        Nov 22 '18 at 5:08











      • @Phil You're right, updated ;)

        – Nick Parsons
        Nov 22 '18 at 5:10

















      That's a lot of repeated DOM querying. I'd just save references to the two <span> elements

      – Phil
      Nov 22 '18 at 5:08





      That's a lot of repeated DOM querying. I'd just save references to the two <span> elements

      – Phil
      Nov 22 '18 at 5:08













      @Phil You're right, updated ;)

      – Nick Parsons
      Nov 22 '18 at 5:10





      @Phil You're right, updated ;)

      – Nick Parsons
      Nov 22 '18 at 5:10













      1














      let p2score = document.querySelector('#p2score').textContent only assigns the value of textContent to the score variable. That value is no longer connected to the element text content property. You must reference the element instead



      const p1button = document
      .querySelector('#p1')
      .addEventListener('click', scoreUp);
      const p2button = document
      .querySelector('#p2')
      .addEventListener('click', scoreUp);

      let p1 = document.querySelector('#p1score');
      let p2 = document.querySelector('#p2score');
      let p1score = p1.textContent;
      let p2score = p2.textContent;

      function scoreUp(e) {
      if (e.target.textContent === 'Player 1') {
      p1.textContent = p1score++;
      console.log(p1.textContent);
      } else {
      p2.textContent = p2score++;
      console.log(p2.textContent);
      }
      }





      share|improve this answer




























        1














        let p2score = document.querySelector('#p2score').textContent only assigns the value of textContent to the score variable. That value is no longer connected to the element text content property. You must reference the element instead



        const p1button = document
        .querySelector('#p1')
        .addEventListener('click', scoreUp);
        const p2button = document
        .querySelector('#p2')
        .addEventListener('click', scoreUp);

        let p1 = document.querySelector('#p1score');
        let p2 = document.querySelector('#p2score');
        let p1score = p1.textContent;
        let p2score = p2.textContent;

        function scoreUp(e) {
        if (e.target.textContent === 'Player 1') {
        p1.textContent = p1score++;
        console.log(p1.textContent);
        } else {
        p2.textContent = p2score++;
        console.log(p2.textContent);
        }
        }





        share|improve this answer


























          1












          1








          1







          let p2score = document.querySelector('#p2score').textContent only assigns the value of textContent to the score variable. That value is no longer connected to the element text content property. You must reference the element instead



          const p1button = document
          .querySelector('#p1')
          .addEventListener('click', scoreUp);
          const p2button = document
          .querySelector('#p2')
          .addEventListener('click', scoreUp);

          let p1 = document.querySelector('#p1score');
          let p2 = document.querySelector('#p2score');
          let p1score = p1.textContent;
          let p2score = p2.textContent;

          function scoreUp(e) {
          if (e.target.textContent === 'Player 1') {
          p1.textContent = p1score++;
          console.log(p1.textContent);
          } else {
          p2.textContent = p2score++;
          console.log(p2.textContent);
          }
          }





          share|improve this answer













          let p2score = document.querySelector('#p2score').textContent only assigns the value of textContent to the score variable. That value is no longer connected to the element text content property. You must reference the element instead



          const p1button = document
          .querySelector('#p1')
          .addEventListener('click', scoreUp);
          const p2button = document
          .querySelector('#p2')
          .addEventListener('click', scoreUp);

          let p1 = document.querySelector('#p1score');
          let p2 = document.querySelector('#p2score');
          let p1score = p1.textContent;
          let p2score = p2.textContent;

          function scoreUp(e) {
          if (e.target.textContent === 'Player 1') {
          p1.textContent = p1score++;
          console.log(p1.textContent);
          } else {
          p2.textContent = p2score++;
          console.log(p2.textContent);
          }
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 '18 at 5:08









          Abana ClaraAbana Clara

          1,646919




          1,646919























              0














              You need to set scores to p1score and p2score elements like so :



              if (e.target.textContent === 'Player 1') {
              p1score++;
              document.querySelector('#p1score').textContent = p1score;
              console.log(p1score);
              } else {
              p2score++;
              document.querySelector('#p2score').textContent = p2score;
              console.log(p2score);
              }





              share|improve this answer




























                0














                You need to set scores to p1score and p2score elements like so :



                if (e.target.textContent === 'Player 1') {
                p1score++;
                document.querySelector('#p1score').textContent = p1score;
                console.log(p1score);
                } else {
                p2score++;
                document.querySelector('#p2score').textContent = p2score;
                console.log(p2score);
                }





                share|improve this answer


























                  0












                  0








                  0







                  You need to set scores to p1score and p2score elements like so :



                  if (e.target.textContent === 'Player 1') {
                  p1score++;
                  document.querySelector('#p1score').textContent = p1score;
                  console.log(p1score);
                  } else {
                  p2score++;
                  document.querySelector('#p2score').textContent = p2score;
                  console.log(p2score);
                  }





                  share|improve this answer













                  You need to set scores to p1score and p2score elements like so :



                  if (e.target.textContent === 'Player 1') {
                  p1score++;
                  document.querySelector('#p1score').textContent = p1score;
                  console.log(p1score);
                  } else {
                  p2score++;
                  document.querySelector('#p2score').textContent = p2score;
                  console.log(p2score);
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 22 '18 at 5:10









                  billbill

                  664




                  664






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424150%2fcant-update-textcontent-of-span-that-is-assigned-to-a-variable-from-within%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

                      SQL update select statement

                      'app-layout' is not a known element: how to share Component with different Modules