Hello!
I’m trying (unsuccessfully ) to modify some code I was given. The goal is to print particular messages when a user enters correct or incorrect information into a table (the table is demonstrating the FOIL method from algebra). Right now, nothing happens! In theory, if an answer is correct, the word “Correct” should appear in the appropriate <td></td> near the input box; if it’s incorrect, 3 different messages are shown. I’d appreciate it if someone could please show me the error of my ways.
Thank you,
Eric
<table id="table1" class="center_table" width="120" border="0" cellspacing="0" cellpadding="5" summary="Showing the Foil Method">
<tr>
<td> </td>
<td class="underline_row" colspan="2" align="center"><span lang="latex">2x+3</span></td>
</tr>
<tr>
<td class="underline_right"><span lang="latex">x</span></td>
<td rowspan="2" align="center"><span lang="latex">2x^2</span></td>
<td class="underline_right" rowspan="2"><input name="" type="text" size="2" maxlength="2" /></td><td name="response" rowspan="2"></td>
</tr>
<tr>
<td class="underline_right"><span lang="latex">+</span></td>
</tr>
<tr>
<td class="underline_right"><span lang="latex">2</span></td>
<td class="underline_row"><input name="" type="text" size="2" maxlength="2" /></td>
<td class="underline_right_corner"><input name="" type="text" size="2" maxlength="2" /></td>
<tr><td> </td><td name="response"></td><td name="response"></td>
</tr>
</table>
var answers1 = new Array("3x","4x","6");
wrong_answers1[0] = "Did you square first?";
wrong_answers1[1] = "Are you sure about that?";
wrong_answers1[2] = "Not quite!";
function checkAnswer(obj,answers,wrong_answers,response){
response.innerHTML = (obj.value == answers[obj.qNum]) ? 'Correct!' : wrong_answers1[obj.qNum];
}
function check_text_questions(text_group,answers,wrong_answers){
var oAnsInputs = document.getElementById(text_group).getElementsByTagName("input");
var oAnsOutputs = document.getElementsByName("response")
for(i=0; i < oAnsInputs.length; i++){
oAnsInputs[i].onchange=function(){checkAnswer(this,answers,wrong_answers,oAnsOutputs[i]);}
oAnsInputs[i].qNum = i;
}
}
check_text_questions('table1',answers1,wrong_answers1);