Using JavaScript to set "FIRST NAME" to titleCase "First Name" | XM Community
Solved

Using JavaScript to set "FIRST NAME" to titleCase "First Name"

  • 7 February 2020
  • 7 replies
  • 90 views

Userlevel 7
Badge +6
Has anyone found a way to fix all caps embedded data, like "NAME", into something like "Name"?

I think this should be doable with piped text, .setEmbeddedData, and a JS expression to convert the piped text to titleCase.

I've tried:

Qualtrics.SurveyEngine.setEmbeddedData('Value_2', titleCase("${e://Field/Value_1")));

But it didn't work. I'm thinking I first need to correct "Value_1" before I can set it to "Value_2.
icon

Best answer by Yash 8 February 2020, 09:08

View original

7 replies

Userlevel 1
Badge +3
Hi,

You can define a function for it and set the value accordingly. Please use below code, it's working fine for me.

var name = "${e://Field/Name}"
var name2 = toTitleCase(name);

function toTitleCase(str) {
return str.replace(
/\\w\\S*/g,
function(txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
}
);
}

Qualtrics.SurveyEngine.setEmbeddedData('Name2',name2);
Userlevel 7
Badge +6
@Yash - You sir, are a scholar and a saint. Works perfectly!
Badge

Yash , I am trying to capitalize all the letters in the text box as the respondents type. Is there a way of doing that? Thank you.

Userlevel 1
Badge +3

Hi ALX, could you please add below code to your javascript block and let me know any difficulty.
Qualtrics.SurveyEngine.addOnReady(function()
{
/*Place your JavaScript here to run when the page is fully displayed*/
jQuery(".InputText").bind('keyup', function (e) {
  if (e.which >= 97 && e.which <= 122) {
    var newKey = e.which - 32;
    e.keyCode = newKey;
    e.charCode = newKey;
  }
  jQuery(".InputText").val((jQuery(".InputText").val()).toUpperCase());
});
});

Badge +6

Hi Yash
Is it possible to capitalize all the letters in the text box as the respondents type for a form field question? I need to ask the first name and last name as individual field questions and I'd prefer to use a form field question. Thank you in advance.

Userlevel 7
Badge +27

https://community.qualtrics.com/XMcommunity/discussion/comment/43138#Comment_43138Yes, use an "input" handler:
jQuery("#"+this.questionId+" .InputText").on("input", function() {
this.value = this.value.toUpperCase();
});

Badge +6

https://community.qualtrics.com/XMcommunity/discussion/comment/43148#Comment_43148Thank you TomG. It's working great. Very much appreciated.

Leave a Reply