Reputation: 77
I Would like rewrite an update a document html, with some info getting from javascript.
<html>
<head>
<title>App</title>
<meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1" />
<script type="text/javascript" src="quiz.js"></script>
</head>
<body>
<form id="test">
<table border="1" cellpadding="5" cellspacing="0">
//q3 and q2, follow the same schema
<tr>
<td>q3?</td>
<td>
<input name="q4" type="radio" value="false"/>
S<br>
<input name="q4" type="radio" value="false"/>
Sa<br>
<input name="q4" type="radio" value="true"/>
Re
</td>
</tr>
</table>
<br/>
<input name="submit" type="button" onClick="gradeTest()" value="Comprueba"/>
</form>
</body>
</html>
And my javascript file, quiz.js
function gradeTest() {
//variables with global implications
var totalQuestions = 3;
var correctAnswers = 0;
var alertText;
var i;
//iterate through all options in radio button list
//if checked value is true, answer is correct
var a2 = document.getElementsByName('q2');
for(i = 0; i < a2.length; i++) {
if(a2[i].checked) {
if(a2[i].value == 'true') {
correctAnswers++;
break;
}
}
}
var a3 = document.getElementsByName('q3');
for(i = 0; i < a3.length; i++) {
if(a3[i].checked) {
if(a3[i].value == 'true') {
correctAnswers++;
break;
}
}
}
var a4 = document.getElementsByName('q4');
for(i = 0; i < a4.length; i++) {
if(a4[i].checked) {
if(a4[i].value == 'true') {
correctAnswers++;
break;
}
}
}
if(correctAnswers==3)
document.write("OK");
The problem was, when i tried to use document.write that generate a new document only with "OK", but i want print this ok in the html with all information.
Upvotes: 1
Views: 2330
Reputation: 21708
You want to append to the DOM. Something like
document.body.appendChild(document.createTextNode('OK'));
See Node.appendChild()
and Document.createTextNode()
.
Upvotes: 2