Difference between revisions of "AutoSum numerical text grid"
(Created page with '== Challenge == We need to sum numbers in a == Example == Image:AutoSum.png == Script == <source> var even = "even"; var AddTotalRowToGrid = true; var UpdateSelfDefi…') |
|||
Line 8: | Line 8: | ||
== Script == | == Script == | ||
− | <source> | + | <source lang="javascript" line="1"> |
var even = "even"; | var even = "even"; | ||
Revision as of 08:58, 24 February 2010
Challenge
We need to sum numbers in a
Example
Script
1 var even = "even";
2
3 var AddTotalRowToGrid = true;
4
5 var UpdateSelfDefinedElement = false;
6
7
8
9 function recalc() {
10
11 var tmpval;
12
13 var tmpnumber = 0;
14
15
16
17 $("input:text").each(
18
19 function(i)
20
21 {
22
23 if ($(this).attr("name").indexOf("QUESTION.") == 0)
24
25 {
26
27 tmpval = $(this).val();
28
29 if(tmpval != "" && !isNaN(tmpval)) {
30
31 //DEBUGGING ALERT FOR GETTING THE ACTUAL VALUE
32
33 //alert("|" + parseInt(tmpval) + "|");
34
35 tmpnumber += parseInt(tmpval);
36
37 }
38
39 }
40
41 }
42
43 );
44
45 if(UpdateSelfDefinedElement)
46
47 $("#AUTOSUMSELFDEFINED").text("Sum Equals: " + tmpnumber);
48
49 if(AddTotalRowToGrid)
50
51 $("#AUTOSUM").text("Sum Equals: " + tmpnumber);
52
53 }
54
55
56
57 quest.onInit = function()
58
59 {
60
61 $("input:text").each(
62
63 function(i)
64
65 {
66
67 if ($(this).attr("name").indexOf("QUESTION.") == 0)
68
69 {
70
71 $(this).keyup(function(){recalc()});
72
73 }
74
75 }
76
77 );
78
79 if(AddTotalRowToGrid) {
80
81 $(".grid_inner").append("<tr rowheight=\"12pt\"><td colspan=2 class=\"grid_subquestion_text grid_subquestion_" + even + "\" style=\"height: 20px;\"><p id=\"AUTOSUM\">Undefined</p></td></tr>");
82
83 }
84
85 recalc();
86
87 }