Search code examples
javascriptjqueryhtmltooltipster

onclick event inside tooltipster


I'm using tooltipster with HTML content and need to hook onclick of an element within the HTML content. my code looks like this:

    $('.groups-tooltip').tooltipster({            
        contentCloning: true,
        trigger: 'custom',
        interactive: true,
        contentAsHTML: true,
        triggerOpen: {
            mouseenter: true
        },
        triggerClose: {
            mouseleave: true
        },

        functionReady: function(instance, helper){   
            $(".tooltip-template-div").hide();
            $('.tooltip-template-span').on("click", function(){ 
                console.log("Clicked!");
            });  

            instance.content($('#tooltip-template').html());
        }      
    });

tooltip-template is just a div container for the tooltip-template-div and tooltip-template-span elemnts.

the

$(".tooltip-template-div").hide();

part is working well but the onclick hook does not seem to have any effect.

Thanks!


Solution

  • You are adding the event listener before inserting the html:

    Try reversing the order

      functionReady: function(instance, helper){ 
           instance.content($('#tooltip-template').html());  
            $(".tooltip-template-div").hide();
            $('.tooltip-template-span').on("click", function(){ 
                console.log("Clicked!");
            });
    
        }