AutoSum numerical text grid

From Catglobe Wiki
Revision as of 08:58, 24 February 2010 by Catglobe (talk | contribs) (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…')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Challenge

We need to sum numbers in a

Example

AutoSum.png

Script

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();

}