Skip to content

Instantly share code, notes, and snippets.

@hinekidori
Created January 1, 2017 23:37
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hinekidori/4085a1222af572057f1058d43cfce773 to your computer and use it in GitHub Desktop.
Save hinekidori/4085a1222af572057f1058d43cfce773 to your computer and use it in GitHub Desktop.
<div id="correctAnswer" style="display:none"></div>
<script>
//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;
};
</script>
@Akon00102329
Copy link

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