1

Multiple values are inputted using below code: $c_num is the count of inputs. There are n number of inputs $stud['stud_c'] is student codes, S01,S02,S05,S08,S19 .........

On a javascript function calculate(), a portion of id is passed.

code:

<input type="number" required id="c_m_<?php echo $c_num; ?>_<?php echo $stud['stud_c'];?>" name="c_m_<?php echo $c_num; ?>[<?php echo $stud['stud_c'];?>]" onkeypress="calculate('<?php echo $c_num; ?>_<?php echo $stud['stud_c'];?>','<?php echo $stud['stud_c'];?>')" class="num<?php echo $c_num; ?> " >

When I alert class1, i am getting corresponding output. But how to store multiple values with different id in array? How to store those inputted values in array num[] ? I also want to alert the total of these inputs.

Corresponding script for finding sum of n number of inputs:

 function calculate(class1,class2){     
    var n = parseFloat(document.getElementById('count').value);
    var num[] = parseFloat(document.getElementById('c_m_'+class1).value);
    var total=0;
    for (var i = 0; i <n; i++) {

       total = total + parseFloat(num[i]) ;
    }
     if (!isNaN(total)) {
       document.getElementById('total_mark_'+class2).value = Math.round(total);

    }
 }
1
  • Couldn't you use JSON? Commented Jan 6, 2017 at 4:34

4 Answers 4

1

Try like this..

<script>
arr =[];
function calculate(class1)
{       
//var n = parseFloat(document.getElementById('count').value);
var num = parseFloat(document.getElementById('c_m_'+class1).value);
arr.push(num);
var total=0;
var n =arr.length;
for (var i = 0; i <n; i++) {

       total = total +arr[i];
}
alert(total);
}
</script>
Sign up to request clarification or add additional context in comments.

Comments

1

Try this . array.push().And array declaration was wrong.use num=[] instead of num[] .

I don't know why use n on forloop length. if you originaly need the array length use with n=num.length

 var num=[];
 function calculate(class1)
{    
var n = parseFloat(document.getElementById('count').value);
 num.push(parseFloat(document.getElementById('c_m_'+class1).value));
 var total=0;
//n=num.length if you need num array length use this
for (var i = 0; i <n; i++) {
      total = total + parseFloat(num[i]) ;
       }
alert(total);
}

Comments

1

Assign a common attribute for the inputs and fetch them by this attribute. There are many ways to fetch nodes by attribute. To name a few:

Example

(function() {
  var inputs, i, total = 0;

  inputs = document.forms.c.querySelectorAll("input[name='c_m[]']");

  for (i = 0; i < inputs.length; i++) {
    total += Number(inputs[i].value);
  }
  console.log(total);
})();
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <title>XXX</title>
</head>
<body>
  <form name="c">
    <input type="text" name="c_m[]" value="1" size="3" />
    <input type="text" name="c_m[]" value="2" size="3" />
    <input type="text" name="c_m[]" value="3" size="3" />
  </form>
</body>
</html>

Note, you don't need to collect the input values into num array, if you only want to compute the sum of the values. Simply literate the nodes and collect their values. If for some reason you still want to collect the values into an array, use Array.prototype.push method:

var num = [];
for (i = 0; i < inputs.length; i++) {
  num.push(inputs[i].value);
}

2 Comments

Good solution..! But if more than one row is there (suppose m rows) and corresponding total (total of row1, total of row 2...total of row m)have to be calculated dynamically when inputting the numbers. what all modifications needed? In this manner all the inputs are summed up. But only row wise summing needed. Thanks in advance for your suggestion
@DayanaBenny, you can iterate the row nodes and apply the same solution to each of them: currentRow.querySelectorAll(...)
0
<script type="text/javascript">
    var RECALCULATE = function(a,b) { 
    var rowtotal = 0;
    n = new Array();
    var count = document.getElementById('criteria_count').value;    

    for (i = 1; i <=count; i++) { 
      if (parseInt(document.getElementById('criteria_mark_'+i+'_'+b).value) > 0) {

           n[i] = parseInt(document.getElementById('criteria_mark_'+i+'_'+b).value);
           rowtotal += n[i];
       }
    }

     document.getElementById('total_mark_'+b).value = rowtotal;
};
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.