Site consists of 3 pages, index.html, 1.html, 2.html.
The goal is to load html from 1.html which contains tooltip and some other htmls and display it in a div, after mouseover on the loaded tooltips I want tooltip texts to be fetched from 2.html.
However this code does not works, no error is shown and AJAX is never executed.
$(document).ready(function(){
$( ".content span" ).tooltip({
track:true,
open: function( event, ui ) {
var id = this.id;
var userid = $(this).attr('data-id');
$.ajax({
url:'2.html',
type:'post',
data:{userid:userid},
success: function(response){
$("#"+id).tooltip('option','content',response);
}
});
}
});
});
.container{
margin: 0 auto;
width: 30%;
}
.content{
border: 1px solid black;
padding: 5px;
margin-bottom: 5px;
}
.content span{
width: 250px;
}
.content span:hover{
cursor: pointer;
}
<!doctype html>
<html>
<head>
<title>Dynamically show data in the Tooltip using AJAX</title>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
</head>
<body>
<input type="button" id="btnLoad" value="Load"/>
<div class='container' id="container"></div>
</body>
<script>
$( "#btnLoad" ).click(function() {
$.ajax({
url: "1.html",
context: document.body
}).done(function( msg ) {
$('#container').html(msg);
$(this).tooltip();
});
});
</script>
</html>
Sample HTML of 1.html file
<div class='content'>
<span title='Please wait..' data-id='1' id='user_1'>Pepsi</span>
</div>
<div class='content'>
<span title='Please wait..' data-id='2' id='user_2'>7 Up</span>
</div>
Sample HTML of 2.html file
This Tooltip text will be replaced with texts from Database
UPDATED
Your updated question is considerably clearer, and helped me track down what is happening. My original answer solved one of the issues, but not all. There are actually a series of problems.
Problem 1
The first problem is the one I previously described: jQuery selectors only match page elements that exist at page load.
$(".content span").tooltip()will only initialise tooltips forspans that exist inside.contentat page load. If you inject newspans later (or new.contents withspans inside them), they are not included, and they won't have tooltips.The simplest fix is to use the (apparently undocumented?)
selectortooltip option. Initialise the tooltips on an HTML element which exists at page load, and then use theselectoroption to filter to only match the elements you really want to have tooltips on. The key is the filtering part happens live, and so will match newly added elements.In your case, we can't use
.contentfor the tooltip, since that does not exist at page load either. We need some parent element which exists on the page before any of your AJAX stuff happens. Let's assume you have a parent like<div class="parent">, and your HTML will be injected into that (I guess you could also usebodyordocument). So we initialise the tooltips on that parent div:Problem 2
Now we have basic tooltip functionality working, even for new content injected in to the page. The next step is to dynamically load content over AJAX, and update the tooltip's title with that content. Normally you would do that like this:
where
selectoris something matching the individualspanwe're currently viewing the tooltip for. But in our case, that won't work - thatspanhas no tooltip attached! Because we're initialising tooltips on.parent, the individualspans where the tooltips show up do not have a tooltip attached, and calling.tooltip()on them will fail with an error like:To reference the existing tooltip, we need to do
$('.parent').tooltip(), but if we do:It would change every tooltip on the page to the same
'New tooltip text!', and we don't want that.The
.openevent handler you are using to fire your AJAX receives the jQueryeventas a parameter. And thateventincludes info about the currently targeted element. Using that, we can find which span on the page we are really currently looking at:So now we can manipulate that element's title:
We can also make it a tooltip!
In fact this is not enough, because the
.parenttooltip is already open and showing "Please wait ...". The above steps do update the title, but you have to move your mouse away and then back to see it. We want the content to refresh immediately, live. I tried some experimentation and found this works well:So we are actually adding new tooltip instances to the page, each time we mouse-over a matching span. This approach has the added advantage that the new tooltip replaces the old, parent one for the specific
span. That means the parent tooltip options no longer apply tospans that have already had their tooltip content loaded, which means if you mouse-over it a 2nd time, it does not fire the AJAX request a 2nd time for thatspan. The content we got from the first AJAX call is already there and simply re-used.Here's a working snippet demonstrating all of this.
I've used https://www.npoint.io/ which is a JSON storage bin, to simulate your AJAX calls. I have no affiliation with it, it just seems like a handy tool. Unfortunately it does not seem very reliable, and requests to it failed a few times while I was working. If that happens, pls retry.
I added a few bins with data matching your
1.htmland2.htmlfiles, the code below loads them and uses their content:Side notes:
I added a tooltip (user ID 0) which is visible on page load, just to demonstrate everything is working both on page load and after new content is injected;
Convention is to use GET when retrieving data, and POST for changing data. Your AJAX calls are just retrieving data to display, so really should be GET;
I wasn't sure why you had
context: document.body, but I don't think it is necessary, and to keep things simple I removed it;If you are going to use a jQuery selector more than once, it makes sense to cache them. I did that for
$parent, and$target.