JavaScript is doing something weird - please help | Experience Community
Skip to main content

JavaScript is doing something weird - please help

  • Mar 2, 2023 5:17 PM UTC
  • 6 replies
  • 45 views

PhilipMoore
Level 2 ●●
Forum|alt.badge.img+11

I'm trying to get something that looks like this:
image.pngBut instead I'm getting this:
image.pngHere is the JS I'm using that is not quite working:
image.pngAny suggestions?

6 replies

TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • Mar 2, 2023 6:12 PM UTC

Replace

"input[type='text'"
with
"input[type=text]"


PhilipMoore
Level 2 ●●
Forum|alt.badge.img+11
  • Author
  • Level 2 ●●
  • Mar 2, 2023 6:59 PM UTC

https://community.qualtrics.com/XMcommunity/discussion/comment/56023#Comment_56023I made this change but it didn't fix the problem. One other thing that seems odd to me is that when I preview the question from within survey builder, things line up
image.pngBut when I preview the full survey, the text entry box is not overlaying the first response bubble and the "$" is showing up in the top left corner of the page
image.png


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • Mar 2, 2023 7:44 PM UTC

The code, where ever you got it, is neither correct nor efficient. Post it as code block so it can be copied and pasted for corrections.


PhilipMoore
Level 2 ●●
Forum|alt.badge.img+11
  • Author
  • Level 2 ●●
  • Mar 2, 2023 8:17 PM UTC

https://community.qualtrics.com/XMcommunity/discussion/comment/56030#Comment_56030Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/


});


Qualtrics.SurveyEngine.addOnReady(function()
{
// Declare variable "text" with value "%"
let text = "$";


// Hide previous element before first input field of type "text"

jQuery("input[type=text]").eq(0).prev().hide();

// To set the container's property to flex
jQuery("input[type=text]").eq(0).parent().css("display", "flex");

// Add a span element with custom styling and text from the "text" variable after the first input field of type "text"

jQuery("input[type=text]").eq(0).before(''+text+'');
});


Qualtrics.SurveyEngine.addOnUnload(function()
{
/*Place your JavaScript here to run when the page is unloaded*/


});


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • Mar 2, 2023 9:45 PM UTC

The key is limiting the element selection to the question. Here is an updated, untested version:
Qualtrics.SurveyEngine.addOnload(function() {
var textin = jQuery("#"+this.questionId+" .InputText").first();
textin.prev().hide();
textin.parent().css("display", "flex");
textin.before('$');
});


PhilipMoore
Level 2 ●●
Forum|alt.badge.img+11
  • Author
  • Level 2 ●●
  • Mar 2, 2023 9:49 PM UTC