Why is my DOM manipulation not working in JavaScript?












1















I am trying to replace the list of paragraphs with only one random paragraph, but for some reason the JavaScript code will not do the job.



I have tried rearranging the variables after the function has ended, but I can't figure out what's wrong.



This is how my HTML elements begin:



<body>
<div id = "quotes">
<p>“<a href="https://theunboundedspirit.com/ananda-coomaraswamy-quotes/">Art</a> is the supreme task and the truly metaphysical activity in this life.”</p>
<p>“Underneath this reality in which we live and have our being, another and altogether different reality lies concealed.”</p>
<p>“We obtain the concept, as we do the form, by overlooking what is individual and actual; whereas nature is acquainted with no forms and no concepts, and likewise with no species, but only with an X which remains inaccessible and undefinable for us.”</p>
<p>“Everything which distinguishes man from the animals depends upon this ability to volatilize perceptual metaphors in a schema, and thus to dissolve an image into a concept.”</p>
<p>“Our destiny exercises its influence over us even when, as yet, we have not learned its nature: it is our future that lays down the law of our today.”</p>


And this is my attempt at DOM manipulation:



"use strict";
const quotes = document.querySelectorAll("p");

const randomize = function() {
let num = (Math.floor(Math.random() * Math.floor(quotes.length)) - 1);
let quote = quotes.item(num).innerHTML;
return quote;
}
let randomQuote = randomize();
let passage = document.getElementById('quotes').innerHTML;
passage = randomQuote;
console.log(randomQuote);









share|improve this question




















  • 1





    You need to assign randomQuote directly to innerHTML to set it. passage = randomQuote; just changes the value of the variable which doesn’t change the DOM at all.

    – Xufox
    Jan 2 at 2:56






  • 1





    Possible duplicate of Modification of innerHTML stored in variable not working

    – Xufox
    Jan 2 at 2:57











  • You are generating one random paragraph in the console. Is this not what you want to do? Your problem is that your equation for getting num is not correct and sometimes results in getting a number that throws an error.

    – ecg8
    Jan 2 at 3:02











  • @ecg8 read the answers (and his question), although his num equation is incorrect OP was also attempting to alter the DOM unsuccessfully

    – Henry Howeson
    Jan 2 at 4:20
















1















I am trying to replace the list of paragraphs with only one random paragraph, but for some reason the JavaScript code will not do the job.



I have tried rearranging the variables after the function has ended, but I can't figure out what's wrong.



This is how my HTML elements begin:



<body>
<div id = "quotes">
<p>“<a href="https://theunboundedspirit.com/ananda-coomaraswamy-quotes/">Art</a> is the supreme task and the truly metaphysical activity in this life.”</p>
<p>“Underneath this reality in which we live and have our being, another and altogether different reality lies concealed.”</p>
<p>“We obtain the concept, as we do the form, by overlooking what is individual and actual; whereas nature is acquainted with no forms and no concepts, and likewise with no species, but only with an X which remains inaccessible and undefinable for us.”</p>
<p>“Everything which distinguishes man from the animals depends upon this ability to volatilize perceptual metaphors in a schema, and thus to dissolve an image into a concept.”</p>
<p>“Our destiny exercises its influence over us even when, as yet, we have not learned its nature: it is our future that lays down the law of our today.”</p>


And this is my attempt at DOM manipulation:



"use strict";
const quotes = document.querySelectorAll("p");

const randomize = function() {
let num = (Math.floor(Math.random() * Math.floor(quotes.length)) - 1);
let quote = quotes.item(num).innerHTML;
return quote;
}
let randomQuote = randomize();
let passage = document.getElementById('quotes').innerHTML;
passage = randomQuote;
console.log(randomQuote);









share|improve this question




















  • 1





    You need to assign randomQuote directly to innerHTML to set it. passage = randomQuote; just changes the value of the variable which doesn’t change the DOM at all.

    – Xufox
    Jan 2 at 2:56






  • 1





    Possible duplicate of Modification of innerHTML stored in variable not working

    – Xufox
    Jan 2 at 2:57











  • You are generating one random paragraph in the console. Is this not what you want to do? Your problem is that your equation for getting num is not correct and sometimes results in getting a number that throws an error.

    – ecg8
    Jan 2 at 3:02











  • @ecg8 read the answers (and his question), although his num equation is incorrect OP was also attempting to alter the DOM unsuccessfully

    – Henry Howeson
    Jan 2 at 4:20














1












1








1








I am trying to replace the list of paragraphs with only one random paragraph, but for some reason the JavaScript code will not do the job.



I have tried rearranging the variables after the function has ended, but I can't figure out what's wrong.



This is how my HTML elements begin:



<body>
<div id = "quotes">
<p>“<a href="https://theunboundedspirit.com/ananda-coomaraswamy-quotes/">Art</a> is the supreme task and the truly metaphysical activity in this life.”</p>
<p>“Underneath this reality in which we live and have our being, another and altogether different reality lies concealed.”</p>
<p>“We obtain the concept, as we do the form, by overlooking what is individual and actual; whereas nature is acquainted with no forms and no concepts, and likewise with no species, but only with an X which remains inaccessible and undefinable for us.”</p>
<p>“Everything which distinguishes man from the animals depends upon this ability to volatilize perceptual metaphors in a schema, and thus to dissolve an image into a concept.”</p>
<p>“Our destiny exercises its influence over us even when, as yet, we have not learned its nature: it is our future that lays down the law of our today.”</p>


And this is my attempt at DOM manipulation:



"use strict";
const quotes = document.querySelectorAll("p");

const randomize = function() {
let num = (Math.floor(Math.random() * Math.floor(quotes.length)) - 1);
let quote = quotes.item(num).innerHTML;
return quote;
}
let randomQuote = randomize();
let passage = document.getElementById('quotes').innerHTML;
passage = randomQuote;
console.log(randomQuote);









share|improve this question
















I am trying to replace the list of paragraphs with only one random paragraph, but for some reason the JavaScript code will not do the job.



I have tried rearranging the variables after the function has ended, but I can't figure out what's wrong.



This is how my HTML elements begin:



<body>
<div id = "quotes">
<p>“<a href="https://theunboundedspirit.com/ananda-coomaraswamy-quotes/">Art</a> is the supreme task and the truly metaphysical activity in this life.”</p>
<p>“Underneath this reality in which we live and have our being, another and altogether different reality lies concealed.”</p>
<p>“We obtain the concept, as we do the form, by overlooking what is individual and actual; whereas nature is acquainted with no forms and no concepts, and likewise with no species, but only with an X which remains inaccessible and undefinable for us.”</p>
<p>“Everything which distinguishes man from the animals depends upon this ability to volatilize perceptual metaphors in a schema, and thus to dissolve an image into a concept.”</p>
<p>“Our destiny exercises its influence over us even when, as yet, we have not learned its nature: it is our future that lays down the law of our today.”</p>


And this is my attempt at DOM manipulation:



"use strict";
const quotes = document.querySelectorAll("p");

const randomize = function() {
let num = (Math.floor(Math.random() * Math.floor(quotes.length)) - 1);
let quote = quotes.item(num).innerHTML;
return quote;
}
let randomQuote = randomize();
let passage = document.getElementById('quotes').innerHTML;
passage = randomQuote;
console.log(randomQuote);






javascript html dom






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 2:56









Dale Burrell

3,27132553




3,27132553










asked Jan 2 at 2:50









ThomasThomas

457




457








  • 1





    You need to assign randomQuote directly to innerHTML to set it. passage = randomQuote; just changes the value of the variable which doesn’t change the DOM at all.

    – Xufox
    Jan 2 at 2:56






  • 1





    Possible duplicate of Modification of innerHTML stored in variable not working

    – Xufox
    Jan 2 at 2:57











  • You are generating one random paragraph in the console. Is this not what you want to do? Your problem is that your equation for getting num is not correct and sometimes results in getting a number that throws an error.

    – ecg8
    Jan 2 at 3:02











  • @ecg8 read the answers (and his question), although his num equation is incorrect OP was also attempting to alter the DOM unsuccessfully

    – Henry Howeson
    Jan 2 at 4:20














  • 1





    You need to assign randomQuote directly to innerHTML to set it. passage = randomQuote; just changes the value of the variable which doesn’t change the DOM at all.

    – Xufox
    Jan 2 at 2:56






  • 1





    Possible duplicate of Modification of innerHTML stored in variable not working

    – Xufox
    Jan 2 at 2:57











  • You are generating one random paragraph in the console. Is this not what you want to do? Your problem is that your equation for getting num is not correct and sometimes results in getting a number that throws an error.

    – ecg8
    Jan 2 at 3:02











  • @ecg8 read the answers (and his question), although his num equation is incorrect OP was also attempting to alter the DOM unsuccessfully

    – Henry Howeson
    Jan 2 at 4:20








1




1





You need to assign randomQuote directly to innerHTML to set it. passage = randomQuote; just changes the value of the variable which doesn’t change the DOM at all.

– Xufox
Jan 2 at 2:56





You need to assign randomQuote directly to innerHTML to set it. passage = randomQuote; just changes the value of the variable which doesn’t change the DOM at all.

– Xufox
Jan 2 at 2:56




1




1





Possible duplicate of Modification of innerHTML stored in variable not working

– Xufox
Jan 2 at 2:57





Possible duplicate of Modification of innerHTML stored in variable not working

– Xufox
Jan 2 at 2:57













You are generating one random paragraph in the console. Is this not what you want to do? Your problem is that your equation for getting num is not correct and sometimes results in getting a number that throws an error.

– ecg8
Jan 2 at 3:02





You are generating one random paragraph in the console. Is this not what you want to do? Your problem is that your equation for getting num is not correct and sometimes results in getting a number that throws an error.

– ecg8
Jan 2 at 3:02













@ecg8 read the answers (and his question), although his num equation is incorrect OP was also attempting to alter the DOM unsuccessfully

– Henry Howeson
Jan 2 at 4:20





@ecg8 read the answers (and his question), although his num equation is incorrect OP was also attempting to alter the DOM unsuccessfully

– Henry Howeson
Jan 2 at 4:20












4 Answers
4






active

oldest

votes


















0














The only way to change the HTML of a node (with innerHTML) is to assign to its innerHTML property, which invokes an internal setter operation. Extracting the innerHTML of a node into a variable and then reassigning that variable won't do anything. (reassigning a variable reference to something else won't ever change anything, by itself.)



So, use



document.getElementById('quotes').innerHTML = randomQuote;


You also need to fix your num random number generator - use Math.floor(Math.random() * quotes.length); to generate a number between 0 and quotes.length - 1, otherwise num will sometimes be -1 (whose index doesn't exist, of course):






"use strict";
const quotes = document.querySelectorAll("p");

const randomize = function() {
const num = Math.floor(Math.random() * quotes.length);
return quotes.item(num).innerHTML;
}
const randomQuote = randomize();
document.getElementById('quotes').innerHTML = randomQuote;

<body>
<div id="quotes">
<p>“<a href="https://theunboundedspirit.com/ananda-coomaraswamy-quotes/">Art</a> is the supreme task and the truly metaphysical activity in this life.”</p>
<p>“Underneath this reality in which we live and have our being, another and altogether different reality lies concealed.”</p>
<p>“We obtain the concept, as we do the form, by overlooking what is individual and actual; whereas nature is acquainted with no forms and no concepts, and likewise with no species, but only with an X which remains inaccessible and undefinable
for us.”</p>
<p>“Everything which distinguishes man from the animals depends upon this ability to volatilize perceptual metaphors in a schema, and thus to dissolve an image into a concept.”</p>
<p>“Our destiny exercises its influence over us even when, as yet, we have not learned its nature: it is our future that lays down the law of our today.”</p>








share|improve this answer































    0














    The problem lies here:



    let passage = document.getElementById('quotes').innerHTML;
    passage = randomQuote;


    You should do:



    let passage = document.getElementById('quotes');
    passage.innerHTML = randomQuote;


    Why



    In this line:



    let passage = document.getElementById('quotes').innerHTML;


    You are actually getting the reference of a string from .innerHTML to passage, not the element itself.



    Therefore, in the next line:



    passage = randomQuote;


    You are only replacing the string with a another string, instead of replacing the property value of an element. Because passage is not an element, it is a string.






    share|improve this answer































      0














      In your example instead of assigning new quote to the innerHtml, you just change the variable with value for it that doesn't keep reference to the innerHtml anymore, just it's value



      Just change this:



      let passage = document.getElementById('quotes').innerHTML;


      to:



      document.getElementById('quotes').innerHTML= randomQuote;





      share|improve this answer































        0














        The issue is that



        let passage = document.getElementById('quotes').innerHTML;


        Sets the value of passage to the instantaneous value of the innerHTML of quotes, it is not a reference (which is not possible in javascript btw).



        passage = randomQuote;


        Just overwrites the value of passage with the random quote. Instead you should write



        document.getElementById('quotes').innerHTML = randomQuote;





        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%2f54000693%2fwhy-is-my-dom-manipulation-not-working-in-javascript%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          The only way to change the HTML of a node (with innerHTML) is to assign to its innerHTML property, which invokes an internal setter operation. Extracting the innerHTML of a node into a variable and then reassigning that variable won't do anything. (reassigning a variable reference to something else won't ever change anything, by itself.)



          So, use



          document.getElementById('quotes').innerHTML = randomQuote;


          You also need to fix your num random number generator - use Math.floor(Math.random() * quotes.length); to generate a number between 0 and quotes.length - 1, otherwise num will sometimes be -1 (whose index doesn't exist, of course):






          "use strict";
          const quotes = document.querySelectorAll("p");

          const randomize = function() {
          const num = Math.floor(Math.random() * quotes.length);
          return quotes.item(num).innerHTML;
          }
          const randomQuote = randomize();
          document.getElementById('quotes').innerHTML = randomQuote;

          <body>
          <div id="quotes">
          <p>“<a href="https://theunboundedspirit.com/ananda-coomaraswamy-quotes/">Art</a> is the supreme task and the truly metaphysical activity in this life.”</p>
          <p>“Underneath this reality in which we live and have our being, another and altogether different reality lies concealed.”</p>
          <p>“We obtain the concept, as we do the form, by overlooking what is individual and actual; whereas nature is acquainted with no forms and no concepts, and likewise with no species, but only with an X which remains inaccessible and undefinable
          for us.”</p>
          <p>“Everything which distinguishes man from the animals depends upon this ability to volatilize perceptual metaphors in a schema, and thus to dissolve an image into a concept.”</p>
          <p>“Our destiny exercises its influence over us even when, as yet, we have not learned its nature: it is our future that lays down the law of our today.”</p>








          share|improve this answer




























            0














            The only way to change the HTML of a node (with innerHTML) is to assign to its innerHTML property, which invokes an internal setter operation. Extracting the innerHTML of a node into a variable and then reassigning that variable won't do anything. (reassigning a variable reference to something else won't ever change anything, by itself.)



            So, use



            document.getElementById('quotes').innerHTML = randomQuote;


            You also need to fix your num random number generator - use Math.floor(Math.random() * quotes.length); to generate a number between 0 and quotes.length - 1, otherwise num will sometimes be -1 (whose index doesn't exist, of course):






            "use strict";
            const quotes = document.querySelectorAll("p");

            const randomize = function() {
            const num = Math.floor(Math.random() * quotes.length);
            return quotes.item(num).innerHTML;
            }
            const randomQuote = randomize();
            document.getElementById('quotes').innerHTML = randomQuote;

            <body>
            <div id="quotes">
            <p>“<a href="https://theunboundedspirit.com/ananda-coomaraswamy-quotes/">Art</a> is the supreme task and the truly metaphysical activity in this life.”</p>
            <p>“Underneath this reality in which we live and have our being, another and altogether different reality lies concealed.”</p>
            <p>“We obtain the concept, as we do the form, by overlooking what is individual and actual; whereas nature is acquainted with no forms and no concepts, and likewise with no species, but only with an X which remains inaccessible and undefinable
            for us.”</p>
            <p>“Everything which distinguishes man from the animals depends upon this ability to volatilize perceptual metaphors in a schema, and thus to dissolve an image into a concept.”</p>
            <p>“Our destiny exercises its influence over us even when, as yet, we have not learned its nature: it is our future that lays down the law of our today.”</p>








            share|improve this answer


























              0












              0








              0







              The only way to change the HTML of a node (with innerHTML) is to assign to its innerHTML property, which invokes an internal setter operation. Extracting the innerHTML of a node into a variable and then reassigning that variable won't do anything. (reassigning a variable reference to something else won't ever change anything, by itself.)



              So, use



              document.getElementById('quotes').innerHTML = randomQuote;


              You also need to fix your num random number generator - use Math.floor(Math.random() * quotes.length); to generate a number between 0 and quotes.length - 1, otherwise num will sometimes be -1 (whose index doesn't exist, of course):






              "use strict";
              const quotes = document.querySelectorAll("p");

              const randomize = function() {
              const num = Math.floor(Math.random() * quotes.length);
              return quotes.item(num).innerHTML;
              }
              const randomQuote = randomize();
              document.getElementById('quotes').innerHTML = randomQuote;

              <body>
              <div id="quotes">
              <p>“<a href="https://theunboundedspirit.com/ananda-coomaraswamy-quotes/">Art</a> is the supreme task and the truly metaphysical activity in this life.”</p>
              <p>“Underneath this reality in which we live and have our being, another and altogether different reality lies concealed.”</p>
              <p>“We obtain the concept, as we do the form, by overlooking what is individual and actual; whereas nature is acquainted with no forms and no concepts, and likewise with no species, but only with an X which remains inaccessible and undefinable
              for us.”</p>
              <p>“Everything which distinguishes man from the animals depends upon this ability to volatilize perceptual metaphors in a schema, and thus to dissolve an image into a concept.”</p>
              <p>“Our destiny exercises its influence over us even when, as yet, we have not learned its nature: it is our future that lays down the law of our today.”</p>








              share|improve this answer













              The only way to change the HTML of a node (with innerHTML) is to assign to its innerHTML property, which invokes an internal setter operation. Extracting the innerHTML of a node into a variable and then reassigning that variable won't do anything. (reassigning a variable reference to something else won't ever change anything, by itself.)



              So, use



              document.getElementById('quotes').innerHTML = randomQuote;


              You also need to fix your num random number generator - use Math.floor(Math.random() * quotes.length); to generate a number between 0 and quotes.length - 1, otherwise num will sometimes be -1 (whose index doesn't exist, of course):






              "use strict";
              const quotes = document.querySelectorAll("p");

              const randomize = function() {
              const num = Math.floor(Math.random() * quotes.length);
              return quotes.item(num).innerHTML;
              }
              const randomQuote = randomize();
              document.getElementById('quotes').innerHTML = randomQuote;

              <body>
              <div id="quotes">
              <p>“<a href="https://theunboundedspirit.com/ananda-coomaraswamy-quotes/">Art</a> is the supreme task and the truly metaphysical activity in this life.”</p>
              <p>“Underneath this reality in which we live and have our being, another and altogether different reality lies concealed.”</p>
              <p>“We obtain the concept, as we do the form, by overlooking what is individual and actual; whereas nature is acquainted with no forms and no concepts, and likewise with no species, but only with an X which remains inaccessible and undefinable
              for us.”</p>
              <p>“Everything which distinguishes man from the animals depends upon this ability to volatilize perceptual metaphors in a schema, and thus to dissolve an image into a concept.”</p>
              <p>“Our destiny exercises its influence over us even when, as yet, we have not learned its nature: it is our future that lays down the law of our today.”</p>








              "use strict";
              const quotes = document.querySelectorAll("p");

              const randomize = function() {
              const num = Math.floor(Math.random() * quotes.length);
              return quotes.item(num).innerHTML;
              }
              const randomQuote = randomize();
              document.getElementById('quotes').innerHTML = randomQuote;

              <body>
              <div id="quotes">
              <p>“<a href="https://theunboundedspirit.com/ananda-coomaraswamy-quotes/">Art</a> is the supreme task and the truly metaphysical activity in this life.”</p>
              <p>“Underneath this reality in which we live and have our being, another and altogether different reality lies concealed.”</p>
              <p>“We obtain the concept, as we do the form, by overlooking what is individual and actual; whereas nature is acquainted with no forms and no concepts, and likewise with no species, but only with an X which remains inaccessible and undefinable
              for us.”</p>
              <p>“Everything which distinguishes man from the animals depends upon this ability to volatilize perceptual metaphors in a schema, and thus to dissolve an image into a concept.”</p>
              <p>“Our destiny exercises its influence over us even when, as yet, we have not learned its nature: it is our future that lays down the law of our today.”</p>





              "use strict";
              const quotes = document.querySelectorAll("p");

              const randomize = function() {
              const num = Math.floor(Math.random() * quotes.length);
              return quotes.item(num).innerHTML;
              }
              const randomQuote = randomize();
              document.getElementById('quotes').innerHTML = randomQuote;

              <body>
              <div id="quotes">
              <p>“<a href="https://theunboundedspirit.com/ananda-coomaraswamy-quotes/">Art</a> is the supreme task and the truly metaphysical activity in this life.”</p>
              <p>“Underneath this reality in which we live and have our being, another and altogether different reality lies concealed.”</p>
              <p>“We obtain the concept, as we do the form, by overlooking what is individual and actual; whereas nature is acquainted with no forms and no concepts, and likewise with no species, but only with an X which remains inaccessible and undefinable
              for us.”</p>
              <p>“Everything which distinguishes man from the animals depends upon this ability to volatilize perceptual metaphors in a schema, and thus to dissolve an image into a concept.”</p>
              <p>“Our destiny exercises its influence over us even when, as yet, we have not learned its nature: it is our future that lays down the law of our today.”</p>






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Jan 2 at 2:57









              CertainPerformanceCertainPerformance

              90.5k165178




              90.5k165178

























                  0














                  The problem lies here:



                  let passage = document.getElementById('quotes').innerHTML;
                  passage = randomQuote;


                  You should do:



                  let passage = document.getElementById('quotes');
                  passage.innerHTML = randomQuote;


                  Why



                  In this line:



                  let passage = document.getElementById('quotes').innerHTML;


                  You are actually getting the reference of a string from .innerHTML to passage, not the element itself.



                  Therefore, in the next line:



                  passage = randomQuote;


                  You are only replacing the string with a another string, instead of replacing the property value of an element. Because passage is not an element, it is a string.






                  share|improve this answer




























                    0














                    The problem lies here:



                    let passage = document.getElementById('quotes').innerHTML;
                    passage = randomQuote;


                    You should do:



                    let passage = document.getElementById('quotes');
                    passage.innerHTML = randomQuote;


                    Why



                    In this line:



                    let passage = document.getElementById('quotes').innerHTML;


                    You are actually getting the reference of a string from .innerHTML to passage, not the element itself.



                    Therefore, in the next line:



                    passage = randomQuote;


                    You are only replacing the string with a another string, instead of replacing the property value of an element. Because passage is not an element, it is a string.






                    share|improve this answer


























                      0












                      0








                      0







                      The problem lies here:



                      let passage = document.getElementById('quotes').innerHTML;
                      passage = randomQuote;


                      You should do:



                      let passage = document.getElementById('quotes');
                      passage.innerHTML = randomQuote;


                      Why



                      In this line:



                      let passage = document.getElementById('quotes').innerHTML;


                      You are actually getting the reference of a string from .innerHTML to passage, not the element itself.



                      Therefore, in the next line:



                      passage = randomQuote;


                      You are only replacing the string with a another string, instead of replacing the property value of an element. Because passage is not an element, it is a string.






                      share|improve this answer













                      The problem lies here:



                      let passage = document.getElementById('quotes').innerHTML;
                      passage = randomQuote;


                      You should do:



                      let passage = document.getElementById('quotes');
                      passage.innerHTML = randomQuote;


                      Why



                      In this line:



                      let passage = document.getElementById('quotes').innerHTML;


                      You are actually getting the reference of a string from .innerHTML to passage, not the element itself.



                      Therefore, in the next line:



                      passage = randomQuote;


                      You are only replacing the string with a another string, instead of replacing the property value of an element. Because passage is not an element, it is a string.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Jan 2 at 2:58









                      Yong QuanYong Quan

                      2,7872927




                      2,7872927























                          0














                          In your example instead of assigning new quote to the innerHtml, you just change the variable with value for it that doesn't keep reference to the innerHtml anymore, just it's value



                          Just change this:



                          let passage = document.getElementById('quotes').innerHTML;


                          to:



                          document.getElementById('quotes').innerHTML= randomQuote;





                          share|improve this answer




























                            0














                            In your example instead of assigning new quote to the innerHtml, you just change the variable with value for it that doesn't keep reference to the innerHtml anymore, just it's value



                            Just change this:



                            let passage = document.getElementById('quotes').innerHTML;


                            to:



                            document.getElementById('quotes').innerHTML= randomQuote;





                            share|improve this answer


























                              0












                              0








                              0







                              In your example instead of assigning new quote to the innerHtml, you just change the variable with value for it that doesn't keep reference to the innerHtml anymore, just it's value



                              Just change this:



                              let passage = document.getElementById('quotes').innerHTML;


                              to:



                              document.getElementById('quotes').innerHTML= randomQuote;





                              share|improve this answer













                              In your example instead of assigning new quote to the innerHtml, you just change the variable with value for it that doesn't keep reference to the innerHtml anymore, just it's value



                              Just change this:



                              let passage = document.getElementById('quotes').innerHTML;


                              to:



                              document.getElementById('quotes').innerHTML= randomQuote;






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Jan 2 at 2:58









                              UmaUma

                              37615




                              37615























                                  0














                                  The issue is that



                                  let passage = document.getElementById('quotes').innerHTML;


                                  Sets the value of passage to the instantaneous value of the innerHTML of quotes, it is not a reference (which is not possible in javascript btw).



                                  passage = randomQuote;


                                  Just overwrites the value of passage with the random quote. Instead you should write



                                  document.getElementById('quotes').innerHTML = randomQuote;





                                  share|improve this answer






























                                    0














                                    The issue is that



                                    let passage = document.getElementById('quotes').innerHTML;


                                    Sets the value of passage to the instantaneous value of the innerHTML of quotes, it is not a reference (which is not possible in javascript btw).



                                    passage = randomQuote;


                                    Just overwrites the value of passage with the random quote. Instead you should write



                                    document.getElementById('quotes').innerHTML = randomQuote;





                                    share|improve this answer




























                                      0












                                      0








                                      0







                                      The issue is that



                                      let passage = document.getElementById('quotes').innerHTML;


                                      Sets the value of passage to the instantaneous value of the innerHTML of quotes, it is not a reference (which is not possible in javascript btw).



                                      passage = randomQuote;


                                      Just overwrites the value of passage with the random quote. Instead you should write



                                      document.getElementById('quotes').innerHTML = randomQuote;





                                      share|improve this answer















                                      The issue is that



                                      let passage = document.getElementById('quotes').innerHTML;


                                      Sets the value of passage to the instantaneous value of the innerHTML of quotes, it is not a reference (which is not possible in javascript btw).



                                      passage = randomQuote;


                                      Just overwrites the value of passage with the random quote. Instead you should write



                                      document.getElementById('quotes').innerHTML = randomQuote;






                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Jan 2 at 3:03

























                                      answered Jan 2 at 2:57









                                      Henry HowesonHenry Howeson

                                      12812




                                      12812






























                                          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%2f54000693%2fwhy-is-my-dom-manipulation-not-working-in-javascript%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

                                          Angular Downloading a file using contenturl with Basic Authentication

                                          Olmecas

                                          Can't read property showImagePicker of undefined in react native iOS