Created
January 1, 2017 23:37
-
-
Save hinekidori/4085a1222af572057f1058d43cfce773 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?