How to make the value of onclicks auto fill in the textbox? | XM Community
Skip to main content
Question

How to make the value of onclicks auto fill in the textbox?

  • September 17, 2019
  • 14 replies
  • 322 views

Hi everyone, I'm working on a survey question (see the screen shot) that allows an observer to count the number of students. So far I can use the code below to make the onclick buttons work. But wondering if a way to make the value automatically filled in the textbox whenever a click occurs? I've Googled for a while but no luck. Really appreciate for any help. Qualtrics.SurveyEngine.addOnload(function() { /*Place your JavaScript here to run when the page loads*/ var k_1 = 0; function clickedb1(n_1) { var P1 = document.getElementsByTagName("P1"); var B_1 = document.getElementById("B_1"); k_1 = k_1 + n_1; B_1.innerHTML = k_1; }; window.onclick; }); !

14 replies

TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • September 17, 2019
Add this line to the top of your script: ``` var input = jQuery("#"+this.questionId+" .InputText"); ``` Add this line to the bottom of your clickedb1 function: ``` input.val(k_1); ```

  • Author
  • September 17, 2019
@TomG Thank you for the quick response! Unfortunately, it's still not showing up in the textbox. Qualtrics.SurveyEngine.addOnload(function() { /*Place your JavaScript here to run when the page loads*/ var input = jQuery("#"+this.questionId+" .InputText"); var k_1 = 0; function clickedb1(n_1) { var P1 = document.getElementsByTagName("P1"); var B_1 = document.getElementById("B_1"); k_1 = k_1 + n_1; B_1.innerHTML = k_1; }; window.onclick; input.val(k_1); });

TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • September 17, 2019

https://www.qualtrics.com/community/discussion/comment/17911#Comment_17911You didn’t put input.val(k_1) inside your clickedb1 function. It should be right after the innerHTML line.


  • Author
  • September 17, 2019
@TomG Sorry I'm new to javascript. Do you mean like this? Qualtrics.SurveyEngine.addOnload(function() { /*Place your JavaScript here to run when the page loads*/ var input = jQuery("#"+this.questionId+" .InputText"); var k_1 = 0; function clickedb1(n_1) { var P1 = document.getElementsByTagName("P1"); var B_1 = document.getElementById("B_1"); k_1 = k_1 + n_1; B_1.innerHTML = k_1; input.val(k_1); }; window.onclick; });

  • Author
  • September 17, 2019
Sorry it's still not working.

TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • September 17, 2019

What type of question are you using?


  • Author
  • September 17, 2019
Text Entry. Should I change to a different question type?

  • Author
  • September 17, 2019
Sorry, my bad. Actually it is matrix table.

TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • September 18, 2019
Change .InputText to input[type=text]

  • Author
  • September 18, 2019
@TomG Unfortunately, still not working. I hope I entered the code correctly. Qualtrics.SurveyEngine.addOnload(function() { /*Place your JavaScript here to run when the page loads*/ var input = jQuery("#"+this.questionId+" .Input[type=text]"); var k_1 = 0; function clickedb1(n_1) { var P1 = document.getElementsByTagName("P1"); var B_1 = document.getElementById("B_1"); k_1 = k_1 + n_1; B_1.innerHTML = k_1; input.val(k_1); }; window.onclick; });

TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • September 18, 2019
Use input[type=text] not .Input[type=text]

  • Author
  • September 18, 2019
I changed "Input" to "input" but still cannot get it working. var input = jQuery("#"+this.questionId+" .input[type=text]"); var k_1 = 0; function clickedb1(n_1) { var P1 = document.getElementsByTagName("P1"); var B_1 = document.getElementById("B_1"); k_1 = k_1 + n_1; B_1.innerHTML = k_1; input.val(k_1); }; window.onclick;

TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • September 18, 2019
Remove the . (period) in front of input.

  • Author
  • September 18, 2019
Still not working... var input = jQuery("#"+this.questionId+" input[type=text]"); var k_1 = 0; function clickedb1(n_1) {