magic_test
magic_test copied to clipboard
Javascript requires inputs to always have a label
This line errors out when the input you've selected with a keypress does not have a corresponding label associated with it. See screenshot:
function keypressFunction(evt) {
evt = evt || window.event;
var charCode = evt.keyCode || evt.which;
var charStr = String.fromCharCode(charCode);
if (!evt.target.labels) {
return;
}
var label = evt.target.labels[0].textContent; // this line errors out when undefined.textContent is accessed
var text = (evt.target.value + charStr).trim().replace("'", "\\\'");
var action = "fill_in";
var target = evt.target.labels[0].textContent;
var options = ", with: '" + text + "'";
https://github.com/bullet-train-co/magic_test/blame/14747324998293e189956c1af15dc07d1e96686b/app/views/magic_test/_javascript_helpers.html#L44
Thoughts? A javascript warning that it doesn't exist would be a decent solution tbh - I only figured this out after I had the console open to figure out why the commands weren't persisting when I invoked flush
in the terminal.
This still affects me, sadly 😂 But it is encouraging me to fix these labels, haha.
I'm curious about this one, it looks like the following is in place to prevent the method from getting to that point in the code:
if (!evt.target.labels) {
return;
}
Maybe we need to do something like !evt.target.labels.length
instead.