Customize Progress Bar to Change Colors Based on Percent Complete | XM Community
Skip to main content
Question

Customize Progress Bar to Change Colors Based on Percent Complete

  • October 12, 2023
  • 6 replies
  • 1604 views

adawson
Level 1 ●
Forum|alt.badge.img+1

Hi, I’ve found code to change the color of the progress bar, however I was wondering if someone could provide a code that would allow the progress bar to move from one color to another based on the percent complete. For example, red > yellow  > green, red for the first 1/3, then it goes into yellow for the second 1/3, and finally green.

 

Thanks!

6 replies

Shashi
Level 8 ●●●●●●●●
Forum|alt.badge.img+32
  • Level 8 ●●●●●●●●
  • 633 replies
  • October 12, 2023

We can use JS to determine the progress % and then using condition we can change progress bar background color as required.


adawson
Level 1 ●
Forum|alt.badge.img+1
  • Author
  • Level 1 ●
  • 6 replies
  • October 12, 2023

Thank you @Shashi, I am new to coding, are you able to provide instructions or the code for JS and the conditions for me to enter? Where would I enter the JS for a progress bar? 


Tom_1842
Level 8 ●●●●●●●●
Forum|alt.badge.img+28
  • Level 8 ●●●●●●●●
  • 876 replies
  • October 13, 2023

Hi, JS can be included in the survey’s Header so that it runs on every page of the survey. Over in the Look & Feel’s General section, click “edit” for the Header and then use the HTML/Source view “<>” to add the below:

<script>

Qualtrics.SurveyEngine.addOnReady(function()
{

var progressbar = document.getElementsByClassName("ProgressBarFill");
var progress = progressbar[0].style.width;
progress = progress.replace(/%/g, '');

if (progress >= 0 && progress < 33) {
  progressbar[0].style.backgroundColor = "#ff0000";
}

if (progress >= 33 && progress < 66) {
  progressbar[0].style.backgroundColor = "#ffff00";
}

if (progress >= 66 && progress < 99) {
  progressbar[0].style.backgroundColor = "#1fd655";
}

if (progress == 100) {
  progressbar[0].style.backgroundColor = "#008631";
}

});

</script>

 


adawson
Level 1 ●
Forum|alt.badge.img+1
  • Author
  • Level 1 ●
  • 6 replies
  • October 13, 2023

Thanks @Tom_1842!
Hm, I placed that code but it appears to only flash the color for a second based on those percents, and then goes back to the default blue color. (I tried to screenshot it, but since the color flashes only for a second, I’m not able to.) Any ideas?


adawson
Level 1 ●
Forum|alt.badge.img+1
  • Author
  • Level 1 ●
  • 6 replies
  • October 13, 2023

Disregard! I changed it to AddOnReady and it’s working as designed. Thanks a million!


Tom_1842
Level 8 ●●●●●●●●
Forum|alt.badge.img+28
  • Level 8 ●●●●●●●●
  • 876 replies
  • October 13, 2023

Glad to hear! I updated my post to use OnReady instead.


Leave a Reply