moth/example-puzzles/example/5/helpers.js

51 lines
1.1 KiB
JavaScript
Raw Normal View History

2019-02-26 16:52:23 -07:00
// jshint asi:true
2019-02-24 15:51:40 -07:00
function helperUpdateAnswer(event) {
let e = event.currentTarget
let value = e.value
let inputs = e.querySelectorAll("input")
if (inputs.length > 0) {
// If there are child input nodes, join their values with commas
let values = []
for (let c of inputs) {
if (c.type == "checkbox") {
if (c.checked) {
values.push(c.value)
}
} else {
values.push(c.value)
}
}
value = values.join(",")
}
// First make any adjustments to the value
if (e.classList.contains("lower")) {
value = value.toLowerCase()
}
if (e.classList.contains("upper")) {
value = value.toUpperCase()
}
2019-02-26 16:52:23 -07:00
let answer = document.querySelector("#answer")
answer.value = value
answer.dispatchEvent(new InputEvent("input"))
2019-02-24 15:51:40 -07:00
}
function helperActivate(e) {
e.addEventListener("input", helperUpdateAnswer)
}
function helperInit(event) {
for (let e of document.querySelectorAll(".answer")) {
helperActivate(e)
}
}
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", helperInit);
} else {
helperInit();
}