Jump to content

Looping Through List Items for Sum?


barkster

Recommended Posts

I have an invocing system that I'm playing with and right now I have several rows of invoice details that have line items with quantity and amounts.  I use ajax to update each row onchange but I have yet figured out a way to update my grand total when that happens.  How can I loop through the list items onchange to get my new total?  Here is an example of the items

 

<div id="rows">
   <ul id="1">
      <li class="col1">Description1</li>
      <li class="col2">1</li>
      <li class="col3">2.50</li>
    </ul>
   <ul id="2">
      <li class="col1">Description2</li>
      <li class="col2">3</li>
      <li class="col3">23.25</li>
   </ul>
</div>

Link to comment
Share on other sites

sorry can't edit but I'm trying to get value of textbox inside the li tag

 

<div id="rows">
   <ul id="1">
      <li class="col1">Description1</li>
      <li class="col2"><input name="qty" type="text" value="1" /></li>
      <li class="col3"><input name="amount" type="text" value="2.5" /></li>
    </ul>
   <ul id="2">
      <li class="col1">Description2</li>
      <li class="col2"><input name="qty" type="text" value="1" /></li>
      <li class="col3"><input name="amount" type="text" value="30" /></li>
   </ul>
</div>

Link to comment
Share on other sites

I think I figured it out but don't know if it is best way

 

function updatetotal() {
var div = document.getElementById('rows');
var obj=div.getElementsByTagName('ul');
var newtotal = 0;
for (var i=0; i<obj.length; i++) {
	console.log(obj[i].getElementsByTagName('LI')[1].firstChild.value);
	console.log(obj[i].getElementsByTagName('LI')[2].firstChild.value);
	//console.log(obj[i].getElementsByTagName('LI')[1].firstChild.nodeValue);//use if you want innerhtml of li tag
	newtotal = newtotal + (obj[i].getElementsByTagName('LI')[1].firstChild.value * obj[i].getElementsByTagName('LI')[2].firstChild.value)
}
console.log("The Grand total is %s",newtotal);
document.getElementById('total').innerHTML = formatCurrency(newtotal);
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.