Toggle menu
876
3.8K
30.2K
279.1K
Catglobe Wiki
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

AutoSum numerical text grid: Difference between revisions

From Catglobe Wiki
No edit summary
Cg loc (talk | contribs)
No edit summary
Line 1: Line 1:
== Challenge  ==
== Challenge  ==


We need to sum numbers in a  
We need to sum numbers in a <br>
 
'''Example'''<br>
== Example ==


[[Image:AutoSum.png]]
[[Image:AutoSum.png]]
 
== Solution ==
== Script ==
*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";
Line 58: Line 59:
}
}
</source>
</source>
== Source ==
Questionnaire Resource Id on cg.catglobe.com site: 164079 (Question: Q7_AutoSum_numerical_text_grid)

Revision as of 02:21, 6 February 2012

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;

function recalc() 
{
   var tmpval;
   var tmpnumber = 0;

   $("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);
}

quest.onInit = function()
{
   $("input:text").each(

      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)