loop through div elements with same class, and getting the values










0














I have the following html:



<input id="box20" name="rooms" value="20" class="box" type="checkbox">
<div>68.00</div> € / night
<input id="box21" name="rooms" value="21" checked="checked" class="box" type="checkbox">
<div class="specialprice">68.00</div>€ / night
<input id="box22" name="rooms" value="22" class="box" type="checkbox">
<div>155.00</div> € / night

<h4>Total: <div class="newprice"></div></h4>


I want to get the values of the div(s) with class specialprice and I want to show the sum in another div named newprice.



I am trying with following javascript, but I am getting blank alert so far:



$(document).ready(function()
$('.specialprice').each(function()
x = $('.specialprice').innerHTML;
alert(x);
);


);



If the class is just , I am not interested to get that value, what I want is to get all prices from divs named specialprice and to make sum from those values.










share|improve this question




























    0














    I have the following html:



    <input id="box20" name="rooms" value="20" class="box" type="checkbox">
    <div>68.00</div> € / night
    <input id="box21" name="rooms" value="21" checked="checked" class="box" type="checkbox">
    <div class="specialprice">68.00</div>€ / night
    <input id="box22" name="rooms" value="22" class="box" type="checkbox">
    <div>155.00</div> € / night

    <h4>Total: <div class="newprice"></div></h4>


    I want to get the values of the div(s) with class specialprice and I want to show the sum in another div named newprice.



    I am trying with following javascript, but I am getting blank alert so far:



    $(document).ready(function()
    $('.specialprice').each(function()
    x = $('.specialprice').innerHTML;
    alert(x);
    );


    );



    If the class is just , I am not interested to get that value, what I want is to get all prices from divs named specialprice and to make sum from those values.










    share|improve this question


























      0












      0








      0







      I have the following html:



      <input id="box20" name="rooms" value="20" class="box" type="checkbox">
      <div>68.00</div> € / night
      <input id="box21" name="rooms" value="21" checked="checked" class="box" type="checkbox">
      <div class="specialprice">68.00</div>€ / night
      <input id="box22" name="rooms" value="22" class="box" type="checkbox">
      <div>155.00</div> € / night

      <h4>Total: <div class="newprice"></div></h4>


      I want to get the values of the div(s) with class specialprice and I want to show the sum in another div named newprice.



      I am trying with following javascript, but I am getting blank alert so far:



      $(document).ready(function()
      $('.specialprice').each(function()
      x = $('.specialprice').innerHTML;
      alert(x);
      );


      );



      If the class is just , I am not interested to get that value, what I want is to get all prices from divs named specialprice and to make sum from those values.










      share|improve this question















      I have the following html:



      <input id="box20" name="rooms" value="20" class="box" type="checkbox">
      <div>68.00</div> € / night
      <input id="box21" name="rooms" value="21" checked="checked" class="box" type="checkbox">
      <div class="specialprice">68.00</div>€ / night
      <input id="box22" name="rooms" value="22" class="box" type="checkbox">
      <div>155.00</div> € / night

      <h4>Total: <div class="newprice"></div></h4>


      I want to get the values of the div(s) with class specialprice and I want to show the sum in another div named newprice.



      I am trying with following javascript, but I am getting blank alert so far:



      $(document).ready(function()
      $('.specialprice').each(function()
      x = $('.specialprice').innerHTML;
      alert(x);
      );


      );



      If the class is just , I am not interested to get that value, what I want is to get all prices from divs named specialprice and to make sum from those values.







      jquery






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 '18 at 4:44









      Cœur

      17.5k9103145




      17.5k9103145










      asked Jul 17 '14 at 7:13









      user2417624user2417624

      191320




      191320






















          3 Answers
          3






          active

          oldest

          votes


















          2














          You are alerting the array:



          $(document).ready(function()
          $('.specialprice').each(function(i, el)
          x = $(el).html();
          alert(x);
          );
          );





          share|improve this answer






















          • I get uncaught type error:undefined is not a function
            – user2417624
            Jul 17 '14 at 7:33










          • Sorry, .html is a method of jQuery, i edited it as x = $(el).html();, try now.
            – Teh SoTo
            Jul 17 '14 at 7:39










          • Hi Teh...now the code works. But how can i get the sum of all checked checkbox? Can you give me a hand with this?
            – user2417624
            Jul 17 '14 at 7:48










          • You want the sum of the html of the divs with class specialprice? Or you want the sum of the value of the checked checkboxes?
            – Teh SoTo
            Jul 17 '14 at 8:06










          • I want the sum of all div's with class special price.
            – user2417624
            Jul 17 '14 at 8:09


















          0














          Like this:



          var addPrice;
          $('.newprice').text(function()
          return $('.specialprice').each(function()
          addPrice += parseInt($(this).text(),10);
          );
          );





          share|improve this answer




















          • No this will return NAN
            – Anto King
            Jul 17 '14 at 7:22










          • Exactly Anto. I just test this code, and returns NaN
            – user2417624
            Jul 17 '14 at 7:31


















          0














          you have to use the $(this) variable to access the current element - Reference to the jQuery API - otherwise you'll have a collection instead of the correct element.



          var prices = , sum = 0;

          $(document).ready(function()
          $('.specialprice').each(function()
          var currentItem = $(this); // Reference to the current item
          prices.push(currentItem.text()); // or .innerHTML, method to get the text


          prices.forEach(function(entry)
          sum += parseInt(entry); // Otherwise it would be a string Concatenation


          $('.newprice').text(sum);
          );





          share|improve this answer




















          • Hi..this code produces uncaught syntax error:unexpected identifier...
            – user2417624
            Jul 17 '14 at 7:36










          • On which line is the error?
            – Jan Biasi
            Jul 17 '14 at 8:44










          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%2f24797088%2floop-through-div-elements-with-same-class-and-getting-the-values%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














          You are alerting the array:



          $(document).ready(function()
          $('.specialprice').each(function(i, el)
          x = $(el).html();
          alert(x);
          );
          );





          share|improve this answer






















          • I get uncaught type error:undefined is not a function
            – user2417624
            Jul 17 '14 at 7:33










          • Sorry, .html is a method of jQuery, i edited it as x = $(el).html();, try now.
            – Teh SoTo
            Jul 17 '14 at 7:39










          • Hi Teh...now the code works. But how can i get the sum of all checked checkbox? Can you give me a hand with this?
            – user2417624
            Jul 17 '14 at 7:48










          • You want the sum of the html of the divs with class specialprice? Or you want the sum of the value of the checked checkboxes?
            – Teh SoTo
            Jul 17 '14 at 8:06










          • I want the sum of all div's with class special price.
            – user2417624
            Jul 17 '14 at 8:09















          2














          You are alerting the array:



          $(document).ready(function()
          $('.specialprice').each(function(i, el)
          x = $(el).html();
          alert(x);
          );
          );





          share|improve this answer






















          • I get uncaught type error:undefined is not a function
            – user2417624
            Jul 17 '14 at 7:33










          • Sorry, .html is a method of jQuery, i edited it as x = $(el).html();, try now.
            – Teh SoTo
            Jul 17 '14 at 7:39










          • Hi Teh...now the code works. But how can i get the sum of all checked checkbox? Can you give me a hand with this?
            – user2417624
            Jul 17 '14 at 7:48










          • You want the sum of the html of the divs with class specialprice? Or you want the sum of the value of the checked checkboxes?
            – Teh SoTo
            Jul 17 '14 at 8:06










          • I want the sum of all div's with class special price.
            – user2417624
            Jul 17 '14 at 8:09













          2












          2








          2






          You are alerting the array:



          $(document).ready(function()
          $('.specialprice').each(function(i, el)
          x = $(el).html();
          alert(x);
          );
          );





          share|improve this answer














          You are alerting the array:



          $(document).ready(function()
          $('.specialprice').each(function(i, el)
          x = $(el).html();
          alert(x);
          );
          );






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jul 17 '14 at 7:38

























          answered Jul 17 '14 at 7:16









          Teh SoToTeh SoTo

          897




          897











          • I get uncaught type error:undefined is not a function
            – user2417624
            Jul 17 '14 at 7:33










          • Sorry, .html is a method of jQuery, i edited it as x = $(el).html();, try now.
            – Teh SoTo
            Jul 17 '14 at 7:39










          • Hi Teh...now the code works. But how can i get the sum of all checked checkbox? Can you give me a hand with this?
            – user2417624
            Jul 17 '14 at 7:48










          • You want the sum of the html of the divs with class specialprice? Or you want the sum of the value of the checked checkboxes?
            – Teh SoTo
            Jul 17 '14 at 8:06










          • I want the sum of all div's with class special price.
            – user2417624
            Jul 17 '14 at 8:09
















          • I get uncaught type error:undefined is not a function
            – user2417624
            Jul 17 '14 at 7:33










          • Sorry, .html is a method of jQuery, i edited it as x = $(el).html();, try now.
            – Teh SoTo
            Jul 17 '14 at 7:39










          • Hi Teh...now the code works. But how can i get the sum of all checked checkbox? Can you give me a hand with this?
            – user2417624
            Jul 17 '14 at 7:48










          • You want the sum of the html of the divs with class specialprice? Or you want the sum of the value of the checked checkboxes?
            – Teh SoTo
            Jul 17 '14 at 8:06










          • I want the sum of all div's with class special price.
            – user2417624
            Jul 17 '14 at 8:09















          I get uncaught type error:undefined is not a function
          – user2417624
          Jul 17 '14 at 7:33




          I get uncaught type error:undefined is not a function
          – user2417624
          Jul 17 '14 at 7:33












          Sorry, .html is a method of jQuery, i edited it as x = $(el).html();, try now.
          – Teh SoTo
          Jul 17 '14 at 7:39




          Sorry, .html is a method of jQuery, i edited it as x = $(el).html();, try now.
          – Teh SoTo
          Jul 17 '14 at 7:39












          Hi Teh...now the code works. But how can i get the sum of all checked checkbox? Can you give me a hand with this?
          – user2417624
          Jul 17 '14 at 7:48




          Hi Teh...now the code works. But how can i get the sum of all checked checkbox? Can you give me a hand with this?
          – user2417624
          Jul 17 '14 at 7:48












          You want the sum of the html of the divs with class specialprice? Or you want the sum of the value of the checked checkboxes?
          – Teh SoTo
          Jul 17 '14 at 8:06




          You want the sum of the html of the divs with class specialprice? Or you want the sum of the value of the checked checkboxes?
          – Teh SoTo
          Jul 17 '14 at 8:06












          I want the sum of all div's with class special price.
          – user2417624
          Jul 17 '14 at 8:09




          I want the sum of all div's with class special price.
          – user2417624
          Jul 17 '14 at 8:09













          0














          Like this:



          var addPrice;
          $('.newprice').text(function()
          return $('.specialprice').each(function()
          addPrice += parseInt($(this).text(),10);
          );
          );





          share|improve this answer




















          • No this will return NAN
            – Anto King
            Jul 17 '14 at 7:22










          • Exactly Anto. I just test this code, and returns NaN
            – user2417624
            Jul 17 '14 at 7:31















          0














          Like this:



          var addPrice;
          $('.newprice').text(function()
          return $('.specialprice').each(function()
          addPrice += parseInt($(this).text(),10);
          );
          );





          share|improve this answer




















          • No this will return NAN
            – Anto King
            Jul 17 '14 at 7:22










          • Exactly Anto. I just test this code, and returns NaN
            – user2417624
            Jul 17 '14 at 7:31













          0












          0








          0






          Like this:



          var addPrice;
          $('.newprice').text(function()
          return $('.specialprice').each(function()
          addPrice += parseInt($(this).text(),10);
          );
          );





          share|improve this answer












          Like this:



          var addPrice;
          $('.newprice').text(function()
          return $('.specialprice').each(function()
          addPrice += parseInt($(this).text(),10);
          );
          );






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jul 17 '14 at 7:19









          Bhojendra RauniyarBhojendra Rauniyar

          50.7k2079125




          50.7k2079125











          • No this will return NAN
            – Anto King
            Jul 17 '14 at 7:22










          • Exactly Anto. I just test this code, and returns NaN
            – user2417624
            Jul 17 '14 at 7:31
















          • No this will return NAN
            – Anto King
            Jul 17 '14 at 7:22










          • Exactly Anto. I just test this code, and returns NaN
            – user2417624
            Jul 17 '14 at 7:31















          No this will return NAN
          – Anto King
          Jul 17 '14 at 7:22




          No this will return NAN
          – Anto King
          Jul 17 '14 at 7:22












          Exactly Anto. I just test this code, and returns NaN
          – user2417624
          Jul 17 '14 at 7:31




          Exactly Anto. I just test this code, and returns NaN
          – user2417624
          Jul 17 '14 at 7:31











          0














          you have to use the $(this) variable to access the current element - Reference to the jQuery API - otherwise you'll have a collection instead of the correct element.



          var prices = , sum = 0;

          $(document).ready(function()
          $('.specialprice').each(function()
          var currentItem = $(this); // Reference to the current item
          prices.push(currentItem.text()); // or .innerHTML, method to get the text


          prices.forEach(function(entry)
          sum += parseInt(entry); // Otherwise it would be a string Concatenation


          $('.newprice').text(sum);
          );





          share|improve this answer




















          • Hi..this code produces uncaught syntax error:unexpected identifier...
            – user2417624
            Jul 17 '14 at 7:36










          • On which line is the error?
            – Jan Biasi
            Jul 17 '14 at 8:44















          0














          you have to use the $(this) variable to access the current element - Reference to the jQuery API - otherwise you'll have a collection instead of the correct element.



          var prices = , sum = 0;

          $(document).ready(function()
          $('.specialprice').each(function()
          var currentItem = $(this); // Reference to the current item
          prices.push(currentItem.text()); // or .innerHTML, method to get the text


          prices.forEach(function(entry)
          sum += parseInt(entry); // Otherwise it would be a string Concatenation


          $('.newprice').text(sum);
          );





          share|improve this answer




















          • Hi..this code produces uncaught syntax error:unexpected identifier...
            – user2417624
            Jul 17 '14 at 7:36










          • On which line is the error?
            – Jan Biasi
            Jul 17 '14 at 8:44













          0












          0








          0






          you have to use the $(this) variable to access the current element - Reference to the jQuery API - otherwise you'll have a collection instead of the correct element.



          var prices = , sum = 0;

          $(document).ready(function()
          $('.specialprice').each(function()
          var currentItem = $(this); // Reference to the current item
          prices.push(currentItem.text()); // or .innerHTML, method to get the text


          prices.forEach(function(entry)
          sum += parseInt(entry); // Otherwise it would be a string Concatenation


          $('.newprice').text(sum);
          );





          share|improve this answer












          you have to use the $(this) variable to access the current element - Reference to the jQuery API - otherwise you'll have a collection instead of the correct element.



          var prices = , sum = 0;

          $(document).ready(function()
          $('.specialprice').each(function()
          var currentItem = $(this); // Reference to the current item
          prices.push(currentItem.text()); // or .innerHTML, method to get the text


          prices.forEach(function(entry)
          sum += parseInt(entry); // Otherwise it would be a string Concatenation


          $('.newprice').text(sum);
          );






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jul 17 '14 at 7:24









          Jan BiasiJan Biasi

          15915




          15915











          • Hi..this code produces uncaught syntax error:unexpected identifier...
            – user2417624
            Jul 17 '14 at 7:36










          • On which line is the error?
            – Jan Biasi
            Jul 17 '14 at 8:44
















          • Hi..this code produces uncaught syntax error:unexpected identifier...
            – user2417624
            Jul 17 '14 at 7:36










          • On which line is the error?
            – Jan Biasi
            Jul 17 '14 at 8:44















          Hi..this code produces uncaught syntax error:unexpected identifier...
          – user2417624
          Jul 17 '14 at 7:36




          Hi..this code produces uncaught syntax error:unexpected identifier...
          – user2417624
          Jul 17 '14 at 7:36












          On which line is the error?
          – Jan Biasi
          Jul 17 '14 at 8:44




          On which line is the error?
          – Jan Biasi
          Jul 17 '14 at 8:44

















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f24797088%2floop-through-div-elements-with-same-class-and-getting-the-values%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

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo