Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
<div id="correctAnswer" style="display:none"></div>
//Script for modifying 'Show Answer' behavior for Input types.
var htmlTextNodes = [];
var innerHTMLText = [];
var htmlNodeLength =document.getElementById('typeans').childNodes.length;
var typedAnswer;
var correctAnswer;
var firstBr = null;
var secondBr;
//capture each node to array
for (i = 0; i < htmlNodeLength; i++) {
htmlTextNodes[i] = document.getElementById('typeans').childNodes[i];
innerHTMLText[i] = document.getElementById('typeans').childNodes[i].innerHTML;
//locate <br> tags for output change markers
if (document.getElementById('typeans').childNodes[i].nodeName == "BR") {
console.log("Runs if BR");
if (firstBr != null) {
secondBr = i;
} else {
firstBr = i;
//If answer is correct, firstBr will still be null, so must set to length of typeans.childNode
if (firstBr == null) {
firstBr = htmlNodeLength;
//assemble typed and correct answer strings
typedAnswer = innerHTMLText.slice(0,firstBr).join("");
var corr = document.getElementById('correctAnswer');
correctAnswer = corr.innerHTML;
//Modify answer output
if (typedAnswer === correctAnswer) {
var c = "<div id='correct'>"+typedAnswer+"</div>";
var d = document.getElementById('typeans');
d.innerHTML = c;
} else {
var e = "<div id='incorrect'>"+typedAnswer+"</div>";
var f = document.getElementById('typeans');
f.innerHTML = e;

This comment has been minimized.

Copy link

@Akon00102329 Akon00102329 commented Apr 7, 2018

This script is very useful, but in some characters (as ñ and other stuff of other languages) does not show in the answer. How I can fix that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment