JQuery hint for dynamic element

I use this tooltip to make a tooltip on an element like

$('.showtooltip').tooltip({
            delay: 0,
            track: true,
            showURL: false,
            bodyHandler: function() {
                var tipStr = "SOME DISPLAY HTML";
                return $(tipStr);
            }
        });

And my ajax will create the element dynamically.

$("<img alt='' class='showtooltip' src='image.jpg' />");

therefore, when this image is added to the document. Tooltip is not displayed. Then I use live () in jquery:

$(".showtooltip").live("mouseover", function() {
                $(this).tooltip({
                    delay: 0,
                    track: true,
                    showURL: false,
                    bodyHandler: function() {
                        var tipStr = "SOME DISPLAY HTML";
                        return $(tipStr);
                    }
                })
            });

But the tooltip is displayed only after the first mouse click on the image. How can I use a tooltip for a dynamic item?

+3
source share
2 answers

I used something like this to reinstall after ajax call. This can help.

$(".showtooltip").live("mouseover",function(){
   if (!$(this).hasClass("tooledUp")){
      $(this).tooltip({
                delay: 0,
                track: true,
                showURL: false,
                bodyHandler: function() {
                    var tipStr = "SOME DISPLAY HTML";
                    return $(tipStr);
                }
      });
      $(this).tooltip().show();
      $(this).addClass("tooledUp");
   }
});

From the forum here: http://static.flowplayer.org/tools/forum/30/37281

0
<input type="button" id="btn" value ="Click here" />

$(document).ready(function(){
$("#btn").click(function(){

    $("<input type='checkbox' title='waaaaw' />").appendTo($(this).parent()).ready(function(){
        $(this).tooltip({
                delay: 0,
                track: true,
                showURL: false,
                bodyHandler: function() {
                    var tipStr = "SOME DISPLAY HTML";
                    return $(tipStr);
                }
          }).addClass("tooledUp").show();
    });
});
});
0

Source: https://habr.com/ru/post/1755572/


All Articles