Count number of attempts at changing a "text entry" field | XM Community
Solved

Count number of attempts at changing a "text entry" field

  • 10 April 2021
  • 3 replies
  • 7 views

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

icon

Best answer by maruiu 12 April 2021, 12:44

View original

3 replies

Userlevel 7
Badge +21

See if this solution works for you: https://www.qualtrics.com/community/discussion/14046/three-validation-tries#latest

Hi ahmedA ,
I'll see if I can adapt the code. The main difference is that by "attempt" I mean only introducing numbers in a box *without submitting them*. There is no button click on which I can condition incrementing the counter, I simply have to count how many times the input in a box changes (and that is where my code does not work).
The reason for this is because even without submitting an input, participants see on the same screen how an image changes dynamically with their input. (But I also want to count inputs which are outside the permitted range, for which the image does not adapt).

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++;
   }


Leave a Reply