How to make the value of onclicks auto fill in the textbox? | XM Community
Skip to main content
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;



});



!
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);

```
@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);



});

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.


@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;









});
Sorry it's still not working.

What type of question are you using?


Text Entry. Should I change to a different question type?
Sorry, my bad. Actually it is matrix table.
Change .InputText to input[type=text]
@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;









});
Use input[type=text] not .Input[type=text]
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;
Remove the . (period) in front of input.
Still not working...



var input = jQuery("#"+this.questionId+" input[type=text]");

var k_1 = 0;

function clickedb1(n_1) {

Leave a Reply