Cannot fetch values from dynamically created textboxes in javascript -
form snapshot 2php code creating textboxes dynamically
for($i=0;$i<count($chkselec);$i++) { echo "<td align='center'><input type='text' id='".$columnvalue[$i]."' name='".$columnvalue[$i]."[]' size='5' onchange=addtoaggr('".$columnvalue[$i]."',".$count_var.") required /></td>"; } echo "<td align='center'><input type='text' id='aggr' name='aggr[]' size='7' value='".$rowtext['totalgrade']."' readonly='readonly' disabled/></td>";
this javascript function
var i=0; var totalgrade=0; function addtoaggr(val,countvar) { var group6 = document.piscoreform.aggr; for(var i=0;i<2;i++) { for(var j=i;j<countvar;j++) { if(val=='comm') { var group1 = document.piscoreform.comm; totalgrade += parsefloat(group1[i].value); } else if(val=='presskil') { var group2 = document.piscoreform.presskil; totalgrade += parsefloat(group2[i].value); alert('2 :'+totalgrade); } else { totalgrade += 0; } } document.getelementsbyname("aggr")[i].value = totalgrade; alert("total " + i+" "+totalgrade); } }
i want find average of each column , display in aggr textbox. in first loop ,the condition i<2 number of panalist. hard-coded value time-being.
for starters document.getelementsbyname call should accept 'aggr[]' , not 'aggr' remove "not object" errors being throw on console.
without having additional context there may additional changes required, here's fix applied you've provided.
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>php code creating textboxes dynamically</title> </head> <body> <?php // mock data $rowtext = array ( 'totalgrade' => 0 ); $columnvalue = array(); for($i=0;$i<5;$i++) { $columnvalue[$i] = "testvalue"; echo "<td align='center'><input type='text' id='".$columnvalue[$i]."' name='".$columnvalue[$i]."[]' size='5' onchange=addtoaggr('".$columnvalue[$i]."',".$i.") required /></td>"; } echo "<td align='center'><input type='text' id='aggr' name='aggr[]' size='7' value='".$rowtext['totalgrade']."' readonly='readonly' disabled/></td>"; ?> <script> var i=0; var totalgrade=0; function addtoaggr(val,countvar) { //var group6 = document.piscoreform.aggr; for(var i=0;i<2;i++) { for(var j=i;j<countvar;j++) { if(val=='comm') { var group1 = document.piscoreform.comm; totalgrade += parsefloat(group1[i].value); } else if(val=='presskil') { var group2 = document.piscoreform.presskil; totalgrade += parsefloat(group2[i].value); alert('2 :'+totalgrade); } else { totalgrade += 0; } } document.getelementsbyname("aggr[]")[i].value = totalgrade; alert("total " + i+" "+totalgrade); } } </script> </body> </html>
Comments
Post a Comment