Color Changing as Slider Question Slides (repost from How to use the survey platform) | XM Community
Question

Color Changing as Slider Question Slides (repost from How to use the survey platform)

  • 1 January 2020
  • 5 replies
  • 89 views

Hello, there. I posted a question in a forum yesterday that I think is more appropriate for this sub-forum.

You can access the original question here, but I'll also detail the specifics to this post.

I would like to use JavaScript to have the background of slider questions (draggable bars) in my survey change from blue (all the way to the left; i.e., 0) to red on the right (i.e., 100) _as the user slides_ the icon.

I would like to accomplish something similar to this colored jQuery UI slider. However, I only want the color to be blue-to-red (not the full range of colors in the provided example). I'd like to be able to do this using JavaScript for specific draggable bars questions because not all questions (sliders will not; bars will) will have this color option.

I am aware of this post on how to change the background image of the slider, but this is _not_ what I want to achieve.

I found a way to get the bar to change in a way that _is close_ to what I desire.

If I use the code:
`var bar = document.querySelectorAll('.bar');
for (var i=0; i < bar.length; i++) {
bar[i].setAttribute("style", "background-image:linear-gradient(-90deg,red,blue)");
}`

I see the slider change from blue to red as I slide across. However, the if I slide all the way to the right I still see blue on the left. I would like the entire bar to change color as the value of the slider bar question increases. For example, when sliding to a value of 25 or less the entire bar would be blue, sliding to 50 would make the entire bar a redish-blue, and sliding to 100 would make the entire bar red. Does that make sense?

5 replies

Userlevel 4
Badge +4
There we go:

function getTheColor( colorVal ) {
var theColor = "";
if ( colorVal < 50 ) {
myRed = 255;
myBlue = parseInt( ( ( colorVal * 2 ) * 255 ) / 100 );
}
else {
myRed = parseInt( ( ( 100 - colorVal ) * 2 ) * 255 / 100 );
myBlue = 255;
}
theColor = "rgb(" + myRed + ",0" + "," + myBlue + ")";
return( theColor );
}

jQuery("input[type = hidden]").change(function () {
sliderScore = parseInt(jQuery("#QID1 input.ResultsInput").eq(0).val())
colorSlider = getTheColor(sliderScore)
jQuery(".track:eq(0)").css({ "background-color":colorSlider});
});
@npetrov937

npetrov937 > @npetrov937 said:
> There we go:
>
> function getTheColor( colorVal ) {
> var theColor = "";
> if ( colorVal < 50 ) {
> myRed = 255;
> myBlue = parseInt( ( ( colorVal * 2 ) * 255 ) / 100 );
> }
> else {
> myRed = parseInt( ( ( 100 - colorVal ) * 2 ) * 255 / 100 );
> myBlue = 255;
> }
> theColor = "rgb(" + myRed + ",0" + "," + myBlue + ")";
> return( theColor );
> }
>
> jQuery("input[type = hidden]").change(function () {
> sliderScore = parseInt(jQuery("#QID1 input.ResultsInput").eq(0).val())
> colorSlider = getTheColor(sliderScore)
> jQuery(".track:eq(0)").css({ "background-color":colorSlider});
> });
>


For some reason, this code does not work for me when I preview the question. I've added the code to the `Qualtrics.SurveyEngine.addOnReady(function()` area of the question javascript code option.
Userlevel 4
Badge +4
@Charlie1099 ,

for me it's under the addOnLoad function - I think this does make a difference.

See attached QSF or link here:
https://greenwichuniversity.eu.qualtrics.com/jfe/form/SV_0d0NlwTYZmbzLkV

P.S. you can also customize slider's starting value so that color appears on load before even dragging it.

Hope that helps!
@npetrov937

Thanks for the quick response. even in the addOnLoad function, this code does not work for the slider Bar style question.

However, I should clarify a couple of things:

1) I would like this to apply to the "bars" slider type not the "sliders" type. Does this matter?

2) I'd like the color to change _up to where the bar has been moved_ rather than the entire bar. I think my original post may have made this unclear.
Here is a link to a preview of two questions providing an example. The first question is attempting the new code. The second question shows a very close attempt. The only difference between the second question and my goal is to change the "gradient" color to be solid that changes at different values of the sliding question.

@npetrov937, I have a similar question. Would really appreciate some help!
The code you shared above works for me, but only for the first slider item (i.e., choice) in the question. How do I modify the code to work for all sliders (i.e., choices) in the question?
I am also looking to get the slider to have color only until the handle and be transparent after the handle (once a respondent activates the handle). Is it possible to do?
Thank you very much!

Leave a Reply