Skip to content

Instantly share code, notes, and snippets.

@Allen-B1
Created January 11, 2019 01:51
Show Gist options
  • Save Allen-B1/6fa0f1801fd09ce0ca1acef349eae9b2 to your computer and use it in GitHub Desktop.
Save Allen-B1/6fa0f1801fd09ce0ca1acef349eae9b2 to your computer and use it in GitHub Desktop.
Colorize text
<html>
<head><title>Colorizer</title></head>
<body>
<input type="text" id="input">
<div id="out"></div>
<script>
rc = () => "#" + (Math.random()).toString(16).slice(-6)
function color(text) {
var out = ""
for (let i = 0; i < text.length; i++) {
out += '<span style="color:' + rc() + '">' + text[i].replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;") + '</span>'
}
return out
}
document.getElementById("input").oninput = function() {
document.getElementById("out").innerHTML = color(this.value)
}
document.onload = function() {
document.getElementById("input").focus()
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment