Hello,
I have a question which is supposed to get participants familiar with a task they have to solve later. The question is a text entry, and they can introduce different numerical values and see how that dynamically changes a result.
I would like to count how many attempts at filling in the text entry box participants make. For now, I would also like to display this number on the same page using html (later I will save it as embedded data, the html is just for me to check whether the value updates as it should).
I came up with the following, which does not work:
In JavaScript, in addOnReady:
var counter = 0;
jQuery("#"+this.questionId+" .InputText").bind('input', function() {
counter++;
});
document.getElementById("counter").innerHTML = counter;
In the HTML of the question, I have the following--before any input, the value of "counter" is -1:
-1
Count number of attempts at changing a "text entry" field
Best answer by maruiu
I figured it out: I created two counters, one for correct attempts (numbers in the required range), and one for incorrect attempts (anything else which is an input, so this excludes pressing keys like backspace, control etc).
Depending on the value of the input in the text entry box, I increment one counter or the other. The total number of attempts is the sum of the two counters.
var inputElement = document.getElementsByTagName("input")[0];
var corr = 0;
var incorr = 0;
/*The function below checks whether a number is an integer.*/
function isInt(x) {
return x % 1 === 0;
}
/*The required range is integers between 0 and 15.*/
if ( inputElement.value >= 0 && inputElement.value <= 15 && isInt(inputElement.value) === true && inputElement.value !== '' ) {
corr++;
}
else if ( inputElement.value < 0 || inputElement.value > 15 || isInt(inputElement.value) === false) {
incorr++;
}
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.
