More actions
No edit summary |
Marked this version for translation |
||
(4 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
== Challenge == | <translate> | ||
== Challenge == <!--T:1--> | |||
We need to sum numbers in a | <!--T:2--> | ||
We need to sum numbers in a <br> | |||
'''Example'''<br> | |||
[[Image:AutoSum.png]] | [[Image:AutoSum.png]] | ||
== Solution == <!--T:3--> | |||
== | *Create a text grid question | ||
*Add the below script to that question | |||
== Code == | |||
<source lang="javascript" line="1"> | <source lang="javascript" line="1"> | ||
var even = "even"; | var even = "even"; | ||
var AddTotalRowToGrid = true; | var AddTotalRowToGrid = true; | ||
var UpdateSelfDefinedElement = false; | var UpdateSelfDefinedElement = false; | ||
<!--T:4--> | |||
function recalc() | |||
function recalc() { | { | ||
var tmpval; | var tmpval; | ||
var tmpnumber = 0; | var tmpnumber = 0; | ||
<!--T:5--> | |||
$("input:text").each( | |||
function(i) | function(i) | ||
{ | { | ||
if ($(this).attr("name").indexOf("QUESTION.") == 0) | if ($(this).attr("name").indexOf("QUESTION.") == 0) | ||
{ | { | ||
tmpval = $(this).val(); | tmpval = $(this).val(); | ||
if(tmpval != "" && !isNaN(tmpval)) { | if(tmpval != "" && !isNaN(tmpval)) { | ||
//DEBUGGING ALERT FOR GETTING THE ACTUAL VALUE | //DEBUGGING ALERT FOR GETTING THE ACTUAL VALUE | ||
//alert("|" + parseInt(tmpval) + "|"); | //alert("|" + parseInt(tmpval) + "|"); | ||
tmpnumber += parseInt(tmpval); | tmpnumber += parseInt(tmpval); | ||
} | } | ||
} | } | ||
} | } | ||
); | ); | ||
if(UpdateSelfDefinedElement) | if(UpdateSelfDefinedElement) | ||
$("#AUTOSUMSELFDEFINED").text("Sum Equals: " + tmpnumber); | $("#AUTOSUMSELFDEFINED").text("Sum Equals: " + tmpnumber); | ||
if(AddTotalRowToGrid) | if(AddTotalRowToGrid) | ||
$("#AUTOSUM").text("Sum Equals: " + tmpnumber); | $("#AUTOSUM").text("Sum Equals: " + tmpnumber); | ||
} | } | ||
<!--T:6--> | |||
quest.onInit = function() | quest.onInit = function() | ||
{ | { | ||
$("input:text").each( | |||
<!--T:7--> | |||
function(i) | function(i) | ||
{ | |||
if ($(this).attr("name").indexOf("QUESTION.") == 0) | |||
{ | |||
$(this).keyup(function(){recalc()}); | |||
} | |||
} | |||
); | |||
if(AddTotalRowToGrid) | |||
if ( | |||
{ | { | ||
$(".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>"); | |||
$( | |||
} | } | ||
recalc(); | |||
recalc(); | |||
} | } | ||
</source> | </source> | ||
== Source == <!--T:8--> | |||
Questionnaire Resource Id on cg.catglobe.com site: 164079 (Question: Q7_AutoSum_numerical_text_grid) | |||
</translate> |
Latest revision as of 07:53, 3 August 2017
<translate>
Challenge
We need to sum numbers in a
Example
Solution
- Create a text grid question
- Add the below script to that question
Code
var even = "even";
var AddTotalRowToGrid = true;
var UpdateSelfDefinedElement = false;
<!--T:4-->
function recalc()
{
var tmpval;
var tmpnumber = 0;
<!--T:5-->
$("input:text").each(
function(i)
{
if ($(this).attr("name").indexOf("QUESTION.") == 0)
{
tmpval = $(this).val();
if(tmpval != "" && !isNaN(tmpval)) {
//DEBUGGING ALERT FOR GETTING THE ACTUAL VALUE
//alert("|" + parseInt(tmpval) + "|");
tmpnumber += parseInt(tmpval);
}
}
}
);
if(UpdateSelfDefinedElement)
$("#AUTOSUMSELFDEFINED").text("Sum Equals: " + tmpnumber);
if(AddTotalRowToGrid)
$("#AUTOSUM").text("Sum Equals: " + tmpnumber);
}
<!--T:6-->
quest.onInit = function()
{
$("input:text").each(
<!--T:7-->
function(i)
{
if ($(this).attr("name").indexOf("QUESTION.") == 0)
{
$(this).keyup(function(){recalc()});
}
}
);
if(AddTotalRowToGrid)
{
$(".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>");
}
recalc();
}
Source
Questionnaire Resource Id on cg.catglobe.com site: 164079 (Question: Q7_AutoSum_numerical_text_grid) </translate>