How to show custom label when respondent hovers over the star rating question? | Experience Community
Skip to main content
Question

How to show custom label when respondent hovers over the star rating question?

  • Jul 27, 2023 5:23 AM UTC
  • 14 replies
  • 404 views

Forum|alt.badge.img+2
  • Level 2 ●●

May I know how to set up custom code to show labels underneath when respondents hover over the star rating question?

I’ve got a 5-star rating question and labels as below:

1 star: Bad

2 stars: Poor

3 stars: Average

4 stars: Good

5 stars: Excellent

14 replies

Shivamvig_sv
Level 6 ●●●●●●
Forum|alt.badge.img+14
  • Level 6 ●●●●●●
  • Jul 27, 2023 5:53 AM UTC

try this
 

 

Qualtrics.SurveyEngine.addOnload(function() {
    var labels = {
        1: "Bad",
        2: "Poor",
        3: "Average",
        4: "Good",
        5: "Excellent"
    };

    function showLabelOnHover(choiceNumber) {
        var choice = document.querySelector('[id^="' + this.questionId + '"][data-choice="' + choiceNumber + '"]');
        var label = document.createElement("div");
        label.innerHTML = labels[choiceNumber];
        label.classList.add("star-label");
        choice.appendChild(label);
    }

    function hideLabelOnHover(choiceNumber) {
        var choice = document.querySelector('[id^="' + this.questionId + '"][data-choice="' + choiceNumber + '"]');
        var label = choice.querySelector(".star-label");
        if (label) {
            choice.removeChild(label);
        }
    }

    var choices = document.querySelectorAll('[id^="' + this.questionId + '"][data-choice]');
    for (var i = 0; i < choices.length; i++) {
        var choiceNumber = parseInt(choices[i].getAttribute("data-choice"));
        choices[i].addEventListener("mouseover", showLabelOnHover.bind(this, choiceNumber));
        choices[i].addEventListener("mouseout", hideLabelOnHover.bind(this, choiceNumber));
    }
});
 


Forum|alt.badge.img+2
  • Author
  • Level 2 ●●
  • Jul 27, 2023 6:08 AM UTC

Thanks for sharing the code! But unfortunately I can’t see any labels showing up in preview upon putting the codes there. 


Forum|alt.badge.img+2
  • Author
  • Level 2 ●●
  • Jul 27, 2023 6:12 AM UTC

May I also ask how to show the labels based on the survey languages? My survey has 3 languages and I would like to show the labels in the respective language.


Shivamvig_sv
Level 6 ●●●●●●
Forum|alt.badge.img+14
  • Level 6 ●●●●●●
  • Jul 27, 2023 6:13 AM UTC

Thanks for sharing the code! But unfortunately I can’t see any labels showing up in preview upon putting the codes there. 

What are you actually getting preview after this code execution.. can you please share a snip?


Forum|alt.badge.img+2
  • Author
  • Level 2 ●●
  • Jul 27, 2023 6:17 AM UTC

This is the screenshot. There’s no label showing up.

 


Forum|alt.badge.img+2
  • Author
  • Level 2 ●●
  • Jul 27, 2023 6:18 AM UTC

This is JS code of the question.

Qualtrics.SurveyEngine.addOnload(function()
{

var labels = {
        1: "Bad",
        2: "Poor",
        3: "Average",
        4: "Good",
        5: "Excellent"
    };

    function showLabelOnHover(choiceNumber) {
        var choice = document.querySelector('[id^="' + this.questionId + '"][data-choice="' + choiceNumber + '"]');
        var label = document.createElement("div");
        label.innerHTML = labels[choiceNumber];
        label.classList.add("star-label");
        choice.appendChild(label);
    }

    function hideLabelOnHover(choiceNumber) {
        var choice = document.querySelector('[id^="' + this.questionId + '"][data-choice="' + choiceNumber + '"]');
        var label = choice.querySelector(".star-label");
        if (label) {
            choice.removeChild(label);
        }
    }

    var choices = document.querySelectorAll('[id^="' + this.questionId + '"][data-choice]');
    for (var i = 0; i < choices.length; i++) {
        var choiceNumber = parseInt(choices[i].getAttribute("data-choice"));
        choices[i].addEventListener("mouseover", showLabelOnHover.bind(this, choiceNumber));
        choices[i].addEventListener("mouseout", hideLabelOnHover.bind(this, choiceNumber));
    }
});

Qualtrics.SurveyEngine.addOnReady(function()
{
    /*Place your JavaScript here to run when the page is fully displayed*/

});

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

});


Shivamvig_sv
Level 6 ●●●●●●
Forum|alt.badge.img+14
  • Level 6 ●●●●●●
  • Jul 27, 2023 6:21 AM UTC

May I also ask how to show the labels based on the survey languages? My survey has 3 languages and I would like to show the labels in the respective language.

Maybe you cab do this by explicitly define the label type in the specific language for the survey.The language selection is determined by the value of an embedded data field named "language." You need to ensure that your survey captures the respondent's language selection and stores it in the "language" embedded data field.

you can use the following JavaScript code to set the "language" embedded data based on the survey language:

Qualtrics.SurveyEngine.addOnload(function() {
    var language = Qualtrics.SurveyEngine.getSurveyLanguage();
    Qualtrics.SurveyEngine.setEmbeddedData("language", language);
});
 


 


Shivamvig_sv
Level 6 ●●●●●●
Forum|alt.badge.img+14
  • Level 6 ●●●●●●
  • Jul 27, 2023 6:22 AM UTC

This is the screenshot. There’s no label showing up.

 

Hover over the stars


Forum|alt.badge.img+2
  • Author
  • Level 2 ●●
  • Jul 27, 2023 6:42 AM UTC

I’ve tried to hover over the stars but no labels showing up. Thanks very much for your sharing on code though. I’ve tried to use the click and show approach with code below and it works for me.

{

jQuery('.StarsEventWatcher').on('touchend click',function(){

jQuery(".SatisfactionIndex").remove();
if($$('.ResultsInput')[0].value == 1){
$(this).parentNode.appendChild(QBuilder('div',{},'Bad'));
}else if($$('.ResultsInput')[0].value == 2){
$(this).parentNode.appendChild(QBuilder('div',{},'Poor'));
}if($$('.ResultsInput')[0].value == 3){
$(this).parentNode.appendChild(QBuilder('div',{},'Average'));
}if($$('.ResultsInput')[0].value == 4){
$(this).parentNode.appendChild(QBuilder('div',{},'Good'));
}if($$('.ResultsInput')[0].value == 5){
$(this).parentNode.appendChild(QBuilder('div',{},'Excellent'));
}
jQuery(".StarsContainer div:last").addClass("SatisfactionIndex");
jQuery(".SatisfactionIndex").css("font-size", "14px");
jQuery(".SatisfactionIndex").css("text-align", "center");
jQuery(".SatisfactionIndex").css("color", "rgb(153, 153, 153)");
    
});
    
});

 

 


Forum|alt.badge.img+2
  • Author
  • Level 2 ●●
  • Jul 27, 2023 6:47 AM UTC

Regarding the language, I used “SelectedLanguage” to control the survey language based on URL like below.

If the “SelectedLanguage” is either “ZH-T” or “ZH-S”, I would like to set the labels as below:

        1: "非常差",
        2: "差",
        3: "普通",
        4: "好",
        5: "非常好"

May I know how should the code be looked like?

 


Shivamvig_sv
Level 6 ●●●●●●
Forum|alt.badge.img+14
  • Level 6 ●●●●●●
  • Jul 27, 2023 12:45 PM UTC

I’ve tried to hover over the stars but no labels showing up. Thanks very much for your sharing on code though. I’ve tried to use the click and show approach with code below and it works for me.

{

jQuery('.StarsEventWatcher').on('touchend click',function(){

jQuery(".SatisfactionIndex").remove();
if($$('.ResultsInput')[0].value == 1){
$(this).parentNode.appendChild(QBuilder('div',{},'Bad'));
}else if($$('.ResultsInput')[0].value == 2){
$(this).parentNode.appendChild(QBuilder('div',{},'Poor'));
}if($$('.ResultsInput')[0].value == 3){
$(this).parentNode.appendChild(QBuilder('div',{},'Average'));
}if($$('.ResultsInput')[0].value == 4){
$(this).parentNode.appendChild(QBuilder('div',{},'Good'));
}if($$('.ResultsInput')[0].value == 5){
$(this).parentNode.appendChild(QBuilder('div',{},'Excellent'));
}
jQuery(".StarsContainer div:last").addClass("SatisfactionIndex");
jQuery(".SatisfactionIndex").css("font-size", "14px");
jQuery(".SatisfactionIndex").css("text-align", "center");
jQuery(".SatisfactionIndex").css("color", "rgb(153, 153, 153)");
    
});
    
});

 

 

That’s a good approach too… surely will help others too then

 


Shivamvig_sv
Level 6 ●●●●●●
Forum|alt.badge.img+14
  • Level 6 ●●●●●●
  • Jul 27, 2023 12:46 PM UTC

Regarding the language, I used “SelectedLanguage” to control the survey language based on URL like below.

If the “SelectedLanguage” is either “ZH-T” or “ZH-S”, I would like to set the labels as below:

        1: "非常差",
        2: "差",
        3: "普通",
        4: "好",
        5: "非常好"

May I know how should the code be looked like?

 

Seems fine… thanks for the knowledgeable inputs 


Forum|alt.badge.img+2
  • Author
  • Level 2 ●●
  • Jul 29, 2023 9:47 AM UTC

Regarding the language, I used “SelectedLanguage” to control the survey language based on URL like below.

If the “SelectedLanguage” is either “ZH-T” or “ZH-S”, I would like to set the labels as below:

        1: "非常差",
        2: "差",
        3: "普通",
        4: "好",
        5: "非常好"

May I know how should the code be looked like?

 

Seems fine… thanks for the knowledgeable inputs 

May I know how to set the JS code to control the label wordings based on “SelectedLanguage”?


Forum|alt.badge.img+2
  • Author
  • Level 2 ●●
  • Jul 30, 2023 11:08 AM UTC

i pasted the code below for the label language but no labels showing up no matter what language. May I know how to update the code to make it work?

{
    var language = Qualtrics.SurveyEngine.getSurveyLanguage();
    Qualtrics.SurveyEngine.setEmbeddedData("language", language);

    jQuery('.StarsEventWatcher').on('touchend click', function () {
        jQuery(".SatisfactionIndex").remove();
        var satisfactionText = '';

        var selectedValue = $$('.ResultsInput')[0].value;
        if (selectedValue == 1) {
            satisfactionText = (language === "ZH-T" || language === "ZH-S") ? '非常差' : 'Bad';
        } else if (selectedValue == 2) {
            satisfactionText = (language === "ZH-T" || language === "ZH-S") ? '差' : 'Poor';
        } else if (selectedValue == 3) {
            satisfactionText = (language === "ZH-T" || language === "ZH-S") ? '一般' : 'Average';
        } else if (selectedValue == 4) {
            satisfactionText = (language === "ZH-T" || language === "ZH-S") ? '好' : 'Good';
        } else if (selectedValue == 5) {
            satisfactionText = (language === "ZH-T" || language === "ZH-S") ? '非常好' : 'Excellent';
        }

        jQuery(this).parent().append(QBuilder('div', {}, satisfactionText));
        jQuery(".StarsContainer div:last").addClass("SatisfactionIndex");
        jQuery(".SatisfactionIndex").css("font-size", "14px");
        jQuery(".SatisfactionIndex").css("text-align", "center");
        jQuery(".SatisfactionIndex").css("color", "rgb(153, 153, 153)");

    });

});