Trying to send this html into localstorage so I can use in a new cart page but it keep giving me an error





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















This is the HTML code:



<div class="cofefe">
<img src="photos/Shop%20-%20French%2Roast.png">
<p>Somecoffee</p>
<p>15</p>
<p>French Roast</p>
<button id="cartitem1" onclick="addCart()" class="button">Add to cart</button>
</div>


This is the javascript:



function addCart() {
var name = this.parentElement.children[1].getContext;
var prices = this.parentElement.children[2].getContext;

var storeThis = {'name': name, 'price': prices};
localStorage.setItem(name, JSON.stringify(storeThis));
}


In the chrome I get :




cart.js:2 Uncaught TypeError: Cannot read property 'children' of undefined
at addCart (cart.js:2)
at HTMLButtonElement.onclick (shop.html:40)




and




Uncaught TypeError: Cannot read property 'children' of undefined




I am sure there is something wrong with the javascript. I don't understand what.










share|improve this question































    0















    This is the HTML code:



    <div class="cofefe">
    <img src="photos/Shop%20-%20French%2Roast.png">
    <p>Somecoffee</p>
    <p>15</p>
    <p>French Roast</p>
    <button id="cartitem1" onclick="addCart()" class="button">Add to cart</button>
    </div>


    This is the javascript:



    function addCart() {
    var name = this.parentElement.children[1].getContext;
    var prices = this.parentElement.children[2].getContext;

    var storeThis = {'name': name, 'price': prices};
    localStorage.setItem(name, JSON.stringify(storeThis));
    }


    In the chrome I get :




    cart.js:2 Uncaught TypeError: Cannot read property 'children' of undefined
    at addCart (cart.js:2)
    at HTMLButtonElement.onclick (shop.html:40)




    and




    Uncaught TypeError: Cannot read property 'children' of undefined




    I am sure there is something wrong with the javascript. I don't understand what.










    share|improve this question



























      0












      0








      0








      This is the HTML code:



      <div class="cofefe">
      <img src="photos/Shop%20-%20French%2Roast.png">
      <p>Somecoffee</p>
      <p>15</p>
      <p>French Roast</p>
      <button id="cartitem1" onclick="addCart()" class="button">Add to cart</button>
      </div>


      This is the javascript:



      function addCart() {
      var name = this.parentElement.children[1].getContext;
      var prices = this.parentElement.children[2].getContext;

      var storeThis = {'name': name, 'price': prices};
      localStorage.setItem(name, JSON.stringify(storeThis));
      }


      In the chrome I get :




      cart.js:2 Uncaught TypeError: Cannot read property 'children' of undefined
      at addCart (cart.js:2)
      at HTMLButtonElement.onclick (shop.html:40)




      and




      Uncaught TypeError: Cannot read property 'children' of undefined




      I am sure there is something wrong with the javascript. I don't understand what.










      share|improve this question
















      This is the HTML code:



      <div class="cofefe">
      <img src="photos/Shop%20-%20French%2Roast.png">
      <p>Somecoffee</p>
      <p>15</p>
      <p>French Roast</p>
      <button id="cartitem1" onclick="addCart()" class="button">Add to cart</button>
      </div>


      This is the javascript:



      function addCart() {
      var name = this.parentElement.children[1].getContext;
      var prices = this.parentElement.children[2].getContext;

      var storeThis = {'name': name, 'price': prices};
      localStorage.setItem(name, JSON.stringify(storeThis));
      }


      In the chrome I get :




      cart.js:2 Uncaught TypeError: Cannot read property 'children' of undefined
      at addCart (cart.js:2)
      at HTMLButtonElement.onclick (shop.html:40)




      and




      Uncaught TypeError: Cannot read property 'children' of undefined




      I am sure there is something wrong with the javascript. I don't understand what.







      javascript jquery html arrays object






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 22 at 12:52









      Jack Bashford

      16.3k41848




      16.3k41848










      asked Nov 24 '18 at 0:23









      DrearoDrearo

      11




      11
























          1 Answer
          1






          active

          oldest

          votes


















          2














          Your JavaScript code has nothing for this to work on. You need to get the current target. Change your function to the below:



          function addCart(event) {
          var name = event.target.parentElement.children[1].getContext;
          var prices = event.target.parentElement.children[2].getContext;
          var storeThis = {'name': name, 'price': prices};
          localStorage.setItem(name, JSON.stringify(storeThis));
          }


          EDIT:



          Here's a failsafe way to make it work - use JavaScript to add an event listener rather than an inline HTML event listener:



          <button id="cartitem1" class="button">Add to cart</button>


          JavaScript:



          document.getElementById("cartitem1").addEventListener("click", function(e) {
          addCart(e);
          })





          share|improve this answer


























          • What is event object? isn't onclick the event? I trried it and it gave me an error : shop.html:40 Uncaught ReferenceError: e is not defined at HTMLButtonElement.onclick (shop.html:40)

            – Drearo
            Nov 24 '18 at 0:41













          • That might happen - I'll show you a way to fix it.

            – Jack Bashford
            Nov 24 '18 at 0:49











          • @Drearo I've fixed it, can you test it to make sure it works for you?

            – Jack Bashford
            Nov 24 '18 at 0:52











          • I had to do document.getitembyid(event)...

            – Drearo
            Nov 26 '18 at 5:01














          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%2f53454171%2ftrying-to-send-this-html-into-localstorage-so-i-can-use-in-a-new-cart-page-but-i%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          Your JavaScript code has nothing for this to work on. You need to get the current target. Change your function to the below:



          function addCart(event) {
          var name = event.target.parentElement.children[1].getContext;
          var prices = event.target.parentElement.children[2].getContext;
          var storeThis = {'name': name, 'price': prices};
          localStorage.setItem(name, JSON.stringify(storeThis));
          }


          EDIT:



          Here's a failsafe way to make it work - use JavaScript to add an event listener rather than an inline HTML event listener:



          <button id="cartitem1" class="button">Add to cart</button>


          JavaScript:



          document.getElementById("cartitem1").addEventListener("click", function(e) {
          addCart(e);
          })





          share|improve this answer


























          • What is event object? isn't onclick the event? I trried it and it gave me an error : shop.html:40 Uncaught ReferenceError: e is not defined at HTMLButtonElement.onclick (shop.html:40)

            – Drearo
            Nov 24 '18 at 0:41













          • That might happen - I'll show you a way to fix it.

            – Jack Bashford
            Nov 24 '18 at 0:49











          • @Drearo I've fixed it, can you test it to make sure it works for you?

            – Jack Bashford
            Nov 24 '18 at 0:52











          • I had to do document.getitembyid(event)...

            – Drearo
            Nov 26 '18 at 5:01


















          2














          Your JavaScript code has nothing for this to work on. You need to get the current target. Change your function to the below:



          function addCart(event) {
          var name = event.target.parentElement.children[1].getContext;
          var prices = event.target.parentElement.children[2].getContext;
          var storeThis = {'name': name, 'price': prices};
          localStorage.setItem(name, JSON.stringify(storeThis));
          }


          EDIT:



          Here's a failsafe way to make it work - use JavaScript to add an event listener rather than an inline HTML event listener:



          <button id="cartitem1" class="button">Add to cart</button>


          JavaScript:



          document.getElementById("cartitem1").addEventListener("click", function(e) {
          addCart(e);
          })





          share|improve this answer


























          • What is event object? isn't onclick the event? I trried it and it gave me an error : shop.html:40 Uncaught ReferenceError: e is not defined at HTMLButtonElement.onclick (shop.html:40)

            – Drearo
            Nov 24 '18 at 0:41













          • That might happen - I'll show you a way to fix it.

            – Jack Bashford
            Nov 24 '18 at 0:49











          • @Drearo I've fixed it, can you test it to make sure it works for you?

            – Jack Bashford
            Nov 24 '18 at 0:52











          • I had to do document.getitembyid(event)...

            – Drearo
            Nov 26 '18 at 5:01
















          2












          2








          2







          Your JavaScript code has nothing for this to work on. You need to get the current target. Change your function to the below:



          function addCart(event) {
          var name = event.target.parentElement.children[1].getContext;
          var prices = event.target.parentElement.children[2].getContext;
          var storeThis = {'name': name, 'price': prices};
          localStorage.setItem(name, JSON.stringify(storeThis));
          }


          EDIT:



          Here's a failsafe way to make it work - use JavaScript to add an event listener rather than an inline HTML event listener:



          <button id="cartitem1" class="button">Add to cart</button>


          JavaScript:



          document.getElementById("cartitem1").addEventListener("click", function(e) {
          addCart(e);
          })





          share|improve this answer















          Your JavaScript code has nothing for this to work on. You need to get the current target. Change your function to the below:



          function addCart(event) {
          var name = event.target.parentElement.children[1].getContext;
          var prices = event.target.parentElement.children[2].getContext;
          var storeThis = {'name': name, 'price': prices};
          localStorage.setItem(name, JSON.stringify(storeThis));
          }


          EDIT:



          Here's a failsafe way to make it work - use JavaScript to add an event listener rather than an inline HTML event listener:



          <button id="cartitem1" class="button">Add to cart</button>


          JavaScript:



          document.getElementById("cartitem1").addEventListener("click", function(e) {
          addCart(e);
          })






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 24 '18 at 0:52

























          answered Nov 24 '18 at 0:31









          Jack BashfordJack Bashford

          16.3k41848




          16.3k41848













          • What is event object? isn't onclick the event? I trried it and it gave me an error : shop.html:40 Uncaught ReferenceError: e is not defined at HTMLButtonElement.onclick (shop.html:40)

            – Drearo
            Nov 24 '18 at 0:41













          • That might happen - I'll show you a way to fix it.

            – Jack Bashford
            Nov 24 '18 at 0:49











          • @Drearo I've fixed it, can you test it to make sure it works for you?

            – Jack Bashford
            Nov 24 '18 at 0:52











          • I had to do document.getitembyid(event)...

            – Drearo
            Nov 26 '18 at 5:01





















          • What is event object? isn't onclick the event? I trried it and it gave me an error : shop.html:40 Uncaught ReferenceError: e is not defined at HTMLButtonElement.onclick (shop.html:40)

            – Drearo
            Nov 24 '18 at 0:41













          • That might happen - I'll show you a way to fix it.

            – Jack Bashford
            Nov 24 '18 at 0:49











          • @Drearo I've fixed it, can you test it to make sure it works for you?

            – Jack Bashford
            Nov 24 '18 at 0:52











          • I had to do document.getitembyid(event)...

            – Drearo
            Nov 26 '18 at 5:01



















          What is event object? isn't onclick the event? I trried it and it gave me an error : shop.html:40 Uncaught ReferenceError: e is not defined at HTMLButtonElement.onclick (shop.html:40)

          – Drearo
          Nov 24 '18 at 0:41







          What is event object? isn't onclick the event? I trried it and it gave me an error : shop.html:40 Uncaught ReferenceError: e is not defined at HTMLButtonElement.onclick (shop.html:40)

          – Drearo
          Nov 24 '18 at 0:41















          That might happen - I'll show you a way to fix it.

          – Jack Bashford
          Nov 24 '18 at 0:49





          That might happen - I'll show you a way to fix it.

          – Jack Bashford
          Nov 24 '18 at 0:49













          @Drearo I've fixed it, can you test it to make sure it works for you?

          – Jack Bashford
          Nov 24 '18 at 0:52





          @Drearo I've fixed it, can you test it to make sure it works for you?

          – Jack Bashford
          Nov 24 '18 at 0:52













          I had to do document.getitembyid(event)...

          – Drearo
          Nov 26 '18 at 5:01







          I had to do document.getitembyid(event)...

          – Drearo
          Nov 26 '18 at 5:01






















          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%2f53454171%2ftrying-to-send-this-html-into-localstorage-so-i-can-use-in-a-new-cart-page-but-i%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







          這個網誌中的熱門文章

          Xamarin.form Move up view when keyboard appear

          Post-Redirect-Get with Spring WebFlux and Thymeleaf

          Anylogic : not able to use stopDelay()