Jump to content

array question


angel777

Recommended Posts

i am afraid i do not get u .. newbie here..

my code is below.. my idea is instead of display all question and counting the total .. i want to split each question by clicking on the next button.. after complete the question.. sum up the total ..  how should i do that 

 

 

script type="text/javascript">

function checkTotal() {

    var total = document.listForm.total;

    var choice = document.listForm.choice;

    var choice1 = document.listForm.choice1;

    var sum = 0;

 

    for (i=0;i<choice.length;i++) {

        if (choice.checked)

            sum = sum + parseInt(choice.value);

    }

 

    for (i=0;i<choice1.length;i++) {

        if (choice1.checked)

            sum = sum + parseInt(choice1.value);

    }

 

  // total.value = sum;

document.listForm.total.value = sum;

}

 

</script>

 

 

 

 

</head>

 

<body>

 

<form name="listForm">

 

<p>2) Do you  feel more self-confident than usual?</p>

<ul>

<Input class="checkbox" type="checkbox" name="choice" value="1"

onclick="checkTotal()">Yes <br>

<Input class="checkbox" type="checkbox" name="choice" value="0"

onclick="checkTotal()">No <br>

</ul>

 

<p>2) Do you  feel happy?</p>

<ul>

<Input class="checkbox" type="checkbox" name="choice1" value="1"

onclick="checkTotal()">Yes <br>

<Input class="checkbox" type="checkbox" name="choice1" value="0"

onclick="checkTotal()">No <br>

</ul>

 

Total: <input type="text" size="2" name="total" value="0"/>

Link to comment
Share on other sites

Always best to learn the code rather than have it given to you, so I won't give you the code here  :D

 

Basically what you'll need to do is store the answer total for each question. You can store it in a site cookie, database, flat file etc. Or you can keep "Posting" it to the page, retrieving it and adding it up as you go along.

 

As far as spliting up the questions, you can use multiple pages, or have all the questions on the same page and have a next button linked to a function that checks what question number you're on, and loads up a new question on each click.

Link to comment
Share on other sites

try this:

 

<script language="javascript">

var i = 0;

var questions = new Array();
questions[1] = "<p>1) Do you  feel more self-confident than usual?</p>"
questions[2] = "<p>2) Do you  feel happy?</p>";
questions[3] = "<p>3) Are you glad I wrote you this awesome script?</p>";
questions[4] = "<p>4) Add more questions in this same fashion.</p>";


function switchThru()
{
i=i+1;
if (i < questions.length) {
document.getElementById("q").innerHTML = questions[i];
document.getElementById("a").innerHTML = "<Input id=\"q1a\" class=\"checkbox\" type=\"checkbox\" value=\"1\" onclick=\"checkTotal(this.id, this.value)\">Yes<Input id=\"q1b\" class=\"checkbox\" type=\"checkbox\" value=\"0\" onclick=\"checkTotal(this.id, this.value)\">No";
}
}


function checkTotal(which,what)
{
var aa = document.getElementById("displayer").value;
var current = document.getElementById(which).value = what;
document.getElementById("displayer").value = (current*1) + (aa*1);
}

window.onload = function() {
switchThru();
}
</script>

<div id="q"></div>
<div id="a"></div>

<br><br>

<input type="text" id="displayer">

<br><br>

<input type="button" value="Next Question" onclick="switchThru()">

Link to comment
Share on other sites

Here is a little bit more of an elaborate version of the original script I wrote for you.

 

<script language="javascript">

var i = 0;

var questions = new Array();
questions[1] = "<p>1) Do you  feel more self-confident than usual?</p>"
questions[2] = "<p>2) Do you  feel happy?</p>";
questions[3] = "<p>3) Aren't you glad I wrote you this awesome script?</p>";
questions[4] = "<p>4) Add more questions in this same fashion.</p>";

function redisable()
{
document.getElementById('rotator').disabled = true;
}

function switchThru()
{
i=i+1;
if (i < questions.length) {
document.getElementById("q").innerHTML = questions[i];
document.getElementById("a").innerHTML = "<Input id=\"q1a\" class=\"checkbox\" type=\"checkbox\" value=\"1\" onclick=\"checkTotal(this.id, this.value)\">Yes<Input id=\"q1b\" class=\"checkbox\" type=\"checkbox\" value=\"0\" onclick=\"checkTotal(this.id, this.value)\">No";
}
else {
document.getElementById("q").innerHTML="You Have Reached The End of This Questionare.";
document.getElementById("a").innerHTML="";
document.getElementById("endnote").innerHTML="Total Score: ";
document.getElementById("buttonwrapper").innerHTML="<span id=\"rotator\"></span>"; 
}
redisable();
}


function checkTotal(which,what)
{
var aa = document.getElementById("displayer").value;
var current = document.getElementById(which).value = what;
var checks = document.getElementById('q1a').checked;
var balances = document.getElementById('q1b').checked;
var total = aa + 1;
if ( aa < i) {
document.getElementById("displayer").value = (current*1) + (aa*1);
}

if (document.getElementById('q1a').checked == true) {
document.getElementById('rotator').disabled = false;
}
else if (document.getElementById('q1b').checked == true) {
document.getElementById('rotator').disabled = false;
}
else if (checks == "false" || balances != "false") {
document.getElementById('rotator').disabled = true;
}
}

window.onload = function() {
switchThru();
document.getElementById("displayer").value="0";
}

window.onreload = function() {
document.getElementById("displayer").value="0";
}

</script>

<div id="q"></div>
<div id="a"></div>

<br><br>

<span id="endnote">Current Score: </span><input type="text" id="displayer" size="5" readonly value="0">

<span id="buttonwrapper">
<br><br>

<input type="button" id="rotator" value="Next Question" onclick="switchThru()" disabled>
</span>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.