This answer works on cells that are not yet generated when this code is run. (jQuery .on()
does not support hover
events.) It also places the tooltip outside (below) the relevant cell, in front of other cells/content.
$('table').on({
mouseover: function(){
var cell = $(this);
if(cell.data('timeout') !== undefined){
clearTimeout(cell.data('timeout'));
cell.removeData('timeout');
showTooltip(cell, false);
}
var timeout = setTimeout(function(){
showTooltip(cell, true);
}, 300); // tooltip delay
cell.data('timeout',timeout);
},
mouseleave: function(){
var cell = $(this);
if(cell.data('timeout') !== undefined){
clearTimeout(cell.data('timeout'));
cell.removeData('timeout');
}
showTooltip(cell, false);
}
}, 'td.hoverTarget');
function showTooltip(cell, show){
if(show){
if(cell.children('.tooltipContainer').length == 0){
var j = cell.parent().children('td').index(cell);
var i = cell.parent().index();
var tooltipContent = generateTooltip(i,j);
if(tooltipContent){
var tooltip = $('<div></div>').addClass('cellTooltip').append(tooltipContent);
var ttc = $('<div></div>').addClass('tooltipContainer').append(tooltip);
cell.append(ttc);
tooltip.hide();
}
}
cell.find('.cellTooltip').fadeIn();
} else {
cell.find('.cellTooltip').fadeOut();
}
}
function generateTooltip(i, j){
// ...
return "HTML for large tooltip.";
}
CSS:
table td.hoverTarget>div.tooltipContainer {
position: relative; /* see https://stackoverflow.com/a/6040258/827280. */
}
table td.hoverTarget>div.tooltipContainer>.cellTooltip {
background-color: white;
border: 1px solid black;
position: absolute;
top: 30px;
z-index: 1;
}