Adding Responsive Tooltips That Work on Mobile Devices | XM Community
Skip to main content

I am trying to add mobile-friendly tooltips using the following instructions: https://osvaldas.info/elegant-css-and-jquery-tooltip-responsive-mobile-friendly
It is working fine on my desktop, but I can't get it to work on mobile devices. I've looked through every community post I could find that uses these instructions and I can't figure out what I'm doing wrong.
Here is what I've added to the Custom CSS Editor under Look and Feel:
#tooltip
{
    text-align: center;
    color: #fff;
    background: #111;
    position: absolute;
    z-index: 100;
    padding: 15px;
}
 
    #tooltip:after /* triangle decoration */
    {
        width: 0;
        height: 0;
        border-left: 10px solid transparent;
        border-right: 10px solid transparent;
        border-top: 10px solid #111;
        content: '';
        position: absolute;
        left: 50%;
        bottom: -10px;
        margin-left: -10px;
    }
 
        #tooltip.top:after
        {
            border-top-color: transparent;
            border-bottom: 10px solid #111;
            top: -20px;
            bottom: auto;
        }
 
        #tooltip.left:after
        {
            left: 10px;
            margin: 0;
        }
 
        #tooltip.right:after
        {
            right: 10px;
            left: auto;
            margin: 0;
        }

In the Edit Question JavaScript section for the question in which I'd like to add the tooltips, I've added the following JS in the onReady section (any instances of $ have been replaced with jQuery):
Qualtrics.SurveyEngine.addOnReady(function()
{
jQuery( function()
{
    var targets = jQuery( 'crel~=tooltip]' ),
        target  = false,
        tooltip = false,
        title   = false;
 
    targets.bind( 'mouseenter', function()
    {
        target  = jQuery( this );
        tip     = target.attr( 'title' );
        tooltip = jQuery( '

' );
 
        if( !tip || tip == '' )
            return false;
 
        target.removeAttr( 'title' );
        tooltip.css( 'opacity', 0 )
               .html( tip )
               .appendTo( 'body' );
 
        var init_tooltip = function()
        {
            if( jQuery( window ).width() < tooltip.outerWidth() * 1.5 )
                tooltip.css( 'max-width', jQuery( window ).width() / 2 );
            else
                tooltip.css( 'max-width', 340 );
 
            var pos_left = target.offset().left + ( target.outerWidth() / 2 ) - ( tooltip.outerWidth() / 2 ),
                pos_top  = target.offset().top - tooltip.outerHeight() - 20;
 
            if( pos_left < 0 )
            {
                pos_left = target.offset().left + target.outerWidth() / 2 - 20;
                tooltip.addClass( 'left' );
            }
            else
                tooltip.removeClass( 'left' );
 
            if( pos_left + tooltip.outerWidth() > jQuery( window ).width() )
            {
                pos_left = target.offset().left - tooltip.outerWidth() + target.outerWidth() / 2 + 20;
                tooltip.addClass( 'right' );
            }
            else
                tooltip.removeClass( 'right' );
 
            if( pos_top < 0 )
            {
                var pos_top  = target.offset().top + target.outerHeight();
                tooltip.addClass( 'top' );
            }
            else
                tooltip.removeClass( 'top' );
 
            tooltip.css( { left: pos_left, top: pos_top } )
                   .animate( { top: '+=10', opacity: 1 }, 50 );
        };
 
        init_tooltip();
        jQuery( window ).resize( init_tooltip );
 
        var remove_tooltip = function()
        {
            tooltip.animate( { top: '-=10', opacity: 0 }, 50, function()
            {
                jQuery( this ).remove();
            });
 
            target.attr( 'title', tip );
        };
 
        target.bind( 'mouseleave', remove_tooltip );
        tooltip.bind( 'click', remove_tooltip );
    });
});


});

I've added the following into the Source section of the Rich Content Editor for the answer choice to which I'm adding the tooltip:
Academics

Hello jflowers1
I added the same code and was able to view the tooltip on mobile. Adding the screenshot below. Not sure where you went wrong.
Screenshot_2022-08-26-22-57-46-69_40deb401b9ffe8e1df2f1cc5ba480b12.jpg


Hi there, I was also able to get the hover text to display on mobile with the code you posted. I have also used a purely CSS+Html tooltip solution before that I liked. I found it here. Options for small, medium, large, xlarge, and fit to element tooltips. Displays as a button to press which I liked for mobile. QSF below if interested.
MobileToolTip.pngHoverText_Different_Sizes.qsf


Hello, I had to download jquery for it to work. Kate 


Leave a Reply