View Full Version : Comparison between relative numbers

07-16-2009, 03:54 PM
I have two tables, as follows:
Question 1 Opt1 Opt2 Opt3 Opt 4 Opt5
Question 2 Opt1 Opt2 Opt3 Opt 4 Opt5
Question n Opt1 Opt2 Opt3 Opt 4 Opt5 TOTAL A

same as above for TABLE B.
I am using this function:
function Somma() {
var sum = 0;
var qn, elem;
for (i=0; i<65; i++) {
qn = 'qn'+i;
elem = document.getElementById(qn);
if (elem.checked == true) { sum += Number(elem.value);
document.getElementById('totaleA').value = sum;and another function SommaB() for TABLE B, with few changes needed.
What I get is two autocalculated and populated fields, totaleA and totaleB. Really I do not need to show the totals.
Since each total can be a positive or negative number I just need a comparison between the two values, with four possible results (++, +-. --, -+).
An user, after answering all questions, should be able to click a button and get his own profile (out of 4 profiles) displayed in a new page.
Maybe this is very basic but I have not been able to find an example code of how to do above, also I am just a newbie in javascript and i do not know how to build a function.
Thanks in advance, and excuse me for my bad (very italian) english.

07-16-2009, 04:35 PM
Do you looking for something like this?

<script type="text/javascript">
var profiles = new Array();
profiles[0] = "profile1.htm"; // ++
profiles[1] = "profile2.htm"; // +-
profiles[2] = "profile3.htm"; // -+
profiles[3] = "profile4.htm"; // --

function loadProfile() {
var totA = parseInt(document.getElementById("totaleA").value);
var totB = parseInt(document.getElementById("totaleB").value);
var prof;
if (totA > 0) {
prof = (totB > 0) ? profiles[0] : profiles[1];
} else {
prof = (totB > 0) ? profiles[2] : profiles[3];
window.location.href = prof;


<form action="#">
<!-- Tables here... -->
<input type="text" id="totaleA" /><br />
<input type="text" id="totaleB" /><br />
<input type="button" value="load profile" onclick="loadProfile();" />

07-16-2009, 07:33 PM
Amphiluke, excellent. It is exactly what I was looking for.
It works with no problems.

Is there a way to "hide" the input field for TotaleA and TotaleB? I do not want the numbers shown to the user. Maybe through CSS?

Thanks again for your help.

07-16-2009, 07:40 PM
Maybe transforming it to hidden fields is the case? Otherwise, set their visibility to "hidden", or set display to "none".

07-29-2009, 12:06 PM
Maybe transforming it to hidden fields is the case? Otherwise, set their visibility to "hidden", or set display to "none".
Hidden fields is fine. Many thanks.

One more question. I use above SommaA() function in this test, as well as other test tables, getting all the totals I need.
Basically i repeat the same function as SommaB() SommaC() and so on.
Is there an elegant and maybe more functional way to do it in a single script, no matter how many sums I must calculate?
This way I could write an external .js file and call the function as many time as needed in different test pages.