Value of selected checkbox

222 views Asked by At

I am trying to find the sum of checkbox values (23.75 and 142.75)

Poaten

Checkbox1: 2012-01-17, Porti, 1.760, 23.75
Checkbox2: 2012-01-17, Kopien, 10.560, 142.55
Checkbox3: 2012-01-17, Honorar, 33.600, 453.60
Checkbox4: 2012-01-17, Telefon, 0.640, 8.65

The output is in "Restbetrag". I used the following function but I receive the sum of primary key's value of selected checkbox items in "Posten". In posten I see four values for each checkbox which are separated by comma. Where should I start to have the 4th value of each clicked checkbox (23.75, 142.55...)? Could you please advice where to find a similar solutions? Thanks mpol

function showTotal() {
    document.frechnungenadd.x_Restbetrag.value = '';
    //document.write("test");
    var sum = 0;

    var elements = document.getElementsByName("x_Posten[]");
    for (i=0;i < elements.length;i++) {
          if (elements[i].checked) {
         sum = sum + +elements[i].value;

  }
}                                                     

    document.frechnungenadd.x_Restbetrag.value = sum;                      
}
2

There are 2 answers

0
user727198 On BEST ANSWER

I solved my problem. Thanks for your support. Here your the code for solution. Regards mpol_ch

function showTotal() {
    document.frechnungenadd.x_Summe.value = ''; 
    document.frechnungenadd.x_MwSt.value = '';
    //document.write("test");
    var Summe = 0;
    var MwSt = 0
    var splitted    
  var elements = document.getElementsByName("x_Posten[]");     
  for (i=0;i < elements.length;i++) {
  if (elements[i].checked) {
     splitted = elements[i].nextSibling.nodeValue.split(",");
     MwSt = MwSt+ + parseFloat(splitted[2]); 
     Summe = Summe+ + parseFloat(splitted[3]);                                                                 
  }                                        
}                                                     
    document.frechnungenadd.x_MwSt.value = MwSt.toFixed(2);                                     
    document.frechnungenadd.x_Summe.value = Summe.toFixed(2);                      
}  
setInterval('showTotal()',1000); 
1
Andy Friese On

Not sure if this is what you are looking for...

if (elements[i].checked) {
   var myarr = elements[i].value.split(",");
   sum += parseFloat(myarr[3]);
}