1 Replies - 478 Views - Last Post: 02 February 2016 - 05:52 PM

#1 bluenew   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 02-February 16

Multiple choice question "alertText" show up in text field, no

Posted 02 February 2016 - 04:24 PM

I have been able to get the following script for a simple multiple choice (and multiple answer) to work for what I need, except instead of "alertText" and a pop-up showing up, I would like the alertText to show up on the html page (e.g. in a text box next to "Check Answer"). Any help would be greatly appreciated.

<script type="text/javascript">
function gradeTest() {
var totalQuestions = 1;
var correctAnswers = 0;
var alertText;
var i;

var a4 = document.getElementsByName('q4');
var a4answers = new Array();
var a4right = new Array('A','C');
var a4bool = true;

for(i = 0; i < a4.length; i++) {
if(a4[i].checked) {
a4answers.push(a4[i].value);
}
}
a4answers.sort();
a4right.sort();
if(a4answers.length == a4right.length) {
for(i = 0; i < a4answers.length; i++) {
if(a4answers[i] != a4right[i]) {
a4bool = false;
break;
}
}
}
else {
a4bool = false;
}
if(a4bool == true) {
correctAnswers++;
}

if(correctAnswers == totalQuestions) {
alertText = "Correct!";
}
else {
alertText = "Sorry, try again!";
}
alert(alertText);
}
</script>
<h1>&nbsp;</h1>
<form id="test">
Which of the following are colors?<br />
<br />
<input name="q4" type="checkbox" id="q4" value="A" />
A) Red?<br />
<input name="q4" type="checkbox" id="q4" value="B" />
B)/>/> Car?<br />
<input name="q4" type="checkbox" id="q4" value="C" />
C)
Blue?<br />
<input name="q4" type="checkbox" id="q4" value="D" />
D) Bike? <br />
<br>
<input name="submit" type="button" onclick="gradeTest()" value="Check Answer"> 
</form>




Is This A Good Question/Topic? 0
  • +

Replies To: Multiple choice question "alertText" show up in text field, no

#2 andrewsw   User is offline

  • RequestedRangeNotSatisfiable
  • member icon

Reputation: 6552
  • View blog
  • Posts: 26,565
  • Joined: 12-December 12

Re: Multiple choice question "alertText" show up in text field, no

Posted 02 February 2016 - 05:52 PM

I believe this was originally posted in the Java forum. Note that Java and Javascript are very different languages.

If you have an existing element then its content can be changed using innerHTML. To create, and insert, a new element you can use createElement and appendChild; there are other methods.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1