Result of a text box calculation DataE (Resultado de un calculo delante del textbox & Dato Embebido) | XM Community
Solved

Result of a text box calculation DataE (Resultado de un calculo delante del textbox & Dato Embebido)


hello I will appreciate your support
I have a text box where a number is entered, and it should calculate a sample size, which should be saved in an embedded data and displayed at the end of four of the same question.
My problem is that my code is activated only if after adding the number I must click on another question and then the script question.
code:
Qualtrics.SurveyEngine.addOnload(function()
{
 var tamuestra;
var inputs = $(this.getQuestionContainer()).select('input[type="text"]');
 var input = inputs[0];
 $(input).insert({before: ' '});
 $(input).insert({after: ' '});
this.questionclick = function(event,element)
  {
     console.log(event, element);
var choiceNum = this.getChoiceValue(1);
if (choiceNum != '')
{
tamuestra = Math.round((0.9604*choiceNum)/((0.0025*(choiceNum-1))+0.9604));
  if (choiceNum < 50)
{
tamuestra = choiceNum;
}
$(input).insert({after: 'Tamaño de muesta: '+tamuestra});
Qualtrics.SurveyEngine.setEmbeddedData("ExternalDataReference", tamuestra);//lo guarda en dato embebido
}
}
});
----------
Tengo un cuadro de texto donde se introduce un número, y debe calcular un tamaño de muestra, que debe guardarse en un dato embebido y mostrarse al final del cuatro de la misma pregunta.
Mi problema es que mi codigo se activa solo si después de agregar el numero debo dar clic en otra pregunta y luego a la pregunta del script.

icon

Best answer by TomG 16 June 2020, 14:52

View original

4 replies

Userlevel 7
Badge +27

There are a number of issues with your code:

  1. "click" isn't the appropriate event. You should be using "blur" or "input"

  2. "this" inside the event handler refers to the event, not the question object. So, this.getChoiceValue() is invalid.

  3. You should convert choiceNum to a number.

  4. inserting 'Tamaño de muesta: ' inside the event handler will add a new occurrence each time the event handler is fired.

  5. You should use a different embedded data name. ExternalReferenceData is specifically for populating an external data reference from a contact list.


Thank you very much for your answer TomG, I was looking for solutions based on your answer, however I still do not find it, I hope you can continue guiding me:
1.     "click" isn't the appropriate event. You should be using "blur" or "input"
Ray- Where can I find documentation and examples to apply this type of event?

2.     "this" inside the event handler refers to the event, not the question object. So, this.getChoiceValue() is invalid.
Ray- I deleted the "this" but the script does not run

3.     You should convert choiceNum to a number. Ray- Change applied thanks

4.     inserting 'Tamaño de muesta: ' inside the event handler will add a new occurrence each time the event handler is fired.
Ray- Will you have a page where I can see examples?

5.     You should use a different embedded data name. ExternalReferenceData is specifically for populating an external data reference from a contact list.
Ray- It's a public survey and I don't have embedded data, but can I use the RecipientFirstName, which is a field I don't use, or is there a way to create a custom embedded data for public surveys?

Userlevel 7
Badge +27

1.     "click" isn't the appropriate event. You should be using "blur" or "input"

Ray- Where can I find documentation and examples to apply this type of event?

Since you are using prototypejs see: http://api.prototypejs.org/dom/Event/on/
2.     "this" inside the event handler refers to the event, not the question object. So, this.getChoiceValue() is invalid.

Ray- I deleted the "this" but the script does not run

Save the value of 'this' as another variable before the event handler (e.g., var qobj = this), then use it within the event handler (e.g., qobj.getChoiceValue())
4.     inserting 'Tamaño de muesta: ' inside the event handler will add a new occurrence each time the event handler is fired.

Ray- Will you have a page where I can see examples?

Create an empty element before the event handler and update its contents with .innerHTML inside the event handler.
5.     You should use a different embedded data name. ExternalReferenceData is specifically for populating an external data reference from a contact list.

Ray- It's a public survey and I don't have embedded data, but can I use the RecipientFirstName, which is a field I don't use, or is there a way to create a custom embedded data for public surveys?

You can name an embedded data variable anything you want (e.g., sampSize). You should avoid using built-in embedded variable names that have a special purpose like ExternalReferenceData and RecipientFirstName.

thanks, I solved it by placing the code in another question that clicking on it brings the data from the previous question and calculates
Qualtrics.SurveyEngine.addOnload(function()
{
var tamuestra=0;
  var answer = Number(document.getElementById('QR~QID5').value);

this.questionclick = function(event,element)
  {
//console.log(event, element);
alert('LAUUU¡¡¡ ');
   answer = Number(document.getElementById('QR~QID5').value);
    
if (answer != '')
{
tamuestra = Math.round((0.9604*answer)/((0.0025*(answer-1))+0.9604));
  if (answer < 50)
{
tamuestra = answer;
}

    alert('Tamaño de muestra '+tamuestra);
Qualtrics.SurveyEngine.setEmbeddedData("RecipientFirstName", tamuestra);//lo guarda en dato embebido
}
}
 
});

Leave a Reply