views:

57

answers:

5

Hi Experts,

This may be a simple question though can´t figure out how to do it.

I am parsing an XML with Jquery Ajax. It contains dates and rates

The XML looks something like

<rate>
 <date>Today</date>
 <price>66</price>
</rate> 
<rate>
 <date>Tomorrow</date>
 <price>99</price>
</rate> 

I simply want to figure out how to calculate the total price of both days Today and Tomorrow. Thought that by using Javascript Number it will simply return the total value of the nodes..

$(xml).find("rate").each(function()
{
   $(this).find("price").each(function()
   {
   $("#TOTALPRICE").append(Number($(this).text()));
   }

}
//output is: 6699 

However, it´s just concatenating the values both not adding them.

//output is: 6699 

I greatly appreciate your help !!

Thanks

+1  A: 

if you just used a javascript variable in the middle it would get you your desired result.

var myTotal = 0;
    $(xml).find("rate").each(function()
    {
       $(this).find("price").each(function()
       {
         mytotal = mytotal  + Number($(this).text());     
       }

    }
  $("#TOTALPRICE"). append(myTotal);
Avitus
A: 

The append doesn't do addition, it will simply add the text. If #TOTALPRICE only contains the total, you can do the following:

$(xml).find("rate").each(function()
{
   $(this).find("price").each(function()
   {
      var current = parseInt($("#TOTALPRICE"));
      current += parseInt($(this).text());
      $("#TOTALPRICE").html(current);
   }
}

If #TOTALPRICE needs the content appended to the end because it contains other content, then the solution posted by Avitus should work for you.

justkt
A: 

jQuery's append will insert content to the end of each element matched by the selector. That means when you are appending '66', that will be added to the element. You need to track the total as Avitus describes, then you probably want to use jQuery's text function to set the content of TOTALPRICE, like this:

$("#TOTALPRICE").text(myTotal); 
rosscj2533
A: 

Guys, Thanks for your answers.

And indeed avitus answer works perfectly well for the above case. Now things are getting harder, I have multiple totals to sum.

Can´t use a single variable, somehow i need to create an array of Totals for each hotel:

By using a variable obviously increases the number on each

Today 66 Tomorrow 99

Any ideas on how to do this ?

Thanks very much in advance !!

Matias
If the answer works, you probably want to accept it by checking the check box next to it. I'd also suggest editing your question with this new information so that everyone who reads the question has the new info. You may also want to up-vote any answers you found helpful.In terms of what you asked, sounds like you know what to do - just turn `myTotal` into an array `myTotals`.
justkt
A: 

Got it guys !!! I Just define the Var inside the "each". That overwrite the value of the variable on each loop:

$(xml).find("rate").each(function()
{
   var myTotal = 0;
   $(this).find("price").each(function()
   {
     mytotal = mytotal  + Number($(this).text());     
   }

}

$("#TOTALPRICE"). append(myTotal);

Thanks very much, this site ABSOLUTELY ROCKS !!!

Matias