JavaScript Button | XM Community
Skip to main content

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.

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!


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.


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



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!


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!


Leave a Reply