JavaScript Button | XM Community
Skip to main content

JavaScript Button

  • November 11, 2022
  • 5 replies
  • 362 views

jmborzick
QPN Level 5 ●●●●●
Forum|alt.badge.img+41

I have a form with several fields (5 questions, each a Form Question with Name and Email) that prepopulate based on data pulling from the directory. I want to add a button to each form question that when clicked it removes the prefilled data and replaces name with "-" and email with "-@email.com." I think this should be possible with JavaScript, but I have been unable to make it work.

5 replies

Deepak
QPN Level 8 ●●●●●●●●
Forum|alt.badge.img+46
  • November 11, 2022

jmborzick
You can follow these steps

  1. Create a button.

  2. Add a placeholder of email.com and - to appropriate form fields and place this onReady

  3. Add an onclick function to replace the value of text box to ''

Hope it helps!


jmborzick
QPN Level 5 ●●●●●
Forum|alt.badge.img+41
  • Author
  • QPN Level 5 ●●●●●
  • November 11, 2022

Deepak, thank you for your response. I was able to create a button to the question, but I don't know how to do steps 2 & 3. My JavaScript knowledge is pre-novice at best.


Deepak
QPN Level 8 ●●●●●●●●
Forum|alt.badge.img+46
  • November 11, 2022

jmborzick
You can create something similar like this for your own Qid and include in HTML. Hope it helps!



jmborzick
QPN Level 5 ●●●●●
Forum|alt.badge.img+41
  • Author
  • QPN Level 5 ●●●●●
  • November 11, 2022

Deepak Thank you, again, for your help. I was able to modify your code to make it work for each field in each of my questions. Thank you!


Deepak
QPN Level 8 ●●●●●●●●
Forum|alt.badge.img+46
  • November 11, 2022

jmborzick
A much cleaner code is to do with IDs and default qid's included in JS and HTML separately.


Qualtrics.SurveyEngine.addOnReady(function()
{

var qid = this.questionId;
var PLACEHOLDER = '@email.com';
  jQuery('.QR-'+qid+'-1').attr('placeholder', PLACEHOLDER);

jQuery('#button1').click(function(){
document.getElementById('QR~'+qid+'~1').value = "";
   
 });
});


Hope it helps!