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

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

  • February 7, 2020
  • 7 replies
  • 171 views

Akdashboard
Level 4 ●●●●
Forum|alt.badge.img+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.

Best answer by Yash

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

7 replies

Yash
Level 1 ●
Forum|alt.badge.img+3
  • Level 1 ●
  • Answer
  • February 8, 2020
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);

Akdashboard
Level 4 ●●●●
Forum|alt.badge.img+6
  • Author
  • Level 4 ●●●●
  • February 10, 2020
@Yash - You sir, are a scholar and a saint. Works perfectly!

Forum|alt.badge.img
  • April 4, 2020

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.


Yash
Level 1 ●
Forum|alt.badge.img+3
  • Level 1 ●
  • April 5, 2020

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());
});
});


nobrien
Level 2 ●●
Forum|alt.badge.img+6
  • Level 2 ●●
  • January 24, 2022

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.


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • January 24, 2022

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();
});


nobrien
Level 2 ●●
Forum|alt.badge.img+6
  • Level 2 ●●
  • January 26, 2022