1

I'm trying to show HTML code inside a tooltip (div, table,...), but it doesn't show up and it doesn't show any errors. If I use the <span> tag with some text inside it, it works correctly. But I need to insert more HTML code inside it. How can I fix it? Only works with <span> tag, and I don't know how to solve this problem, any help? Why is this happening?

$(document).ready(function() {
 //Tooltips
 $(".tip_trigger").hover(function(){
  tip = $(this).find('.tip');
  tip.show(); //Show tooltip
 }, function() {
  tip.hide(); //Hide tooltip    
 }).mousemove(function(e) {
  var mousex = e.pageX + 20; //Get X coodrinates
  var mousey = e.pageY + 20; //Get Y coordinates
  var tipWidth = tip.width(); //Find width of tooltip
  var tipHeight = tip.height(); //Find height of tooltip
  
  //Distance of element from the right edge of viewport
  var tipVisX = $(window).width() - (mousex + tipWidth);
  //Distance of element from the bottom of viewport
  var tipVisY = $(window).height() - (mousey + tipHeight);
    
  if ( tipVisX < 20 ) { //If tooltip exceeds the X coordinate of viewport
   mousex = e.pageX - tipWidth - 20;
  } if ( tipVisY < 20 ) { //If tooltip exceeds the Y coordinate of viewport
   mousey = e.pageY - tipHeight - 20;
  } 
  tip.css({  top: mousey, left: mousex });
 });
});
body {
 margin: 0; padding: 0;
 font: normal 12px Verdana, Geneva, sans-serif;
 line-height: 1.8em;
 color: #333;
}
 
* {
 outline: none;
}
img {border: none;}

a {color: #d60000; text-decoration: none;}

/*--Tooltip Styles--*/
.tip {
 display: inline-block;
 color: #fff;
 background:#1d1d1d;
 display:none; /*--Hides by default--*/
 padding:10px;
 position:absolute; z-index:1000;
 -webkit-border-radius: 3px;
 -moz-border-radius: 3px;
 border-radius: 3px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<p style="text-align:left;">My First
  <a href="#" class="tip_trigger">Link 
   <span class="tip">
    Some text. Works correctly!
   </span>
  </a>
</p>
<p style="text-align:left;">My Second
  <a href="#" class="tip_trigger">Link 
   <div class="tip">
    <table><tr><td>More text. Doesn't work!</td></tr></table>
   </div>
  </a>
</p>
Maistrenko Vitalii
  • 994
  • 1
  • 8
  • 16
Norak
  • 383
  • 2
  • 6
  • 14

1 Answers1

2

Your issue is with the type of elements you put inside of other elements.

For example:

You can not put a <div> inside of an <a> or a <p>

See here: Putting <div> inside <p> is adding an extra <p>

I updated your example code with a working one.

$(document).ready(function() {
 //Tooltips
 $(".tip_trigger").hover(function(){
  tip = $(this).siblings('.tip');
  tip.show(); //Show tooltip
 }, function() {
  tip.hide(); //Hide tooltip    
 }).mousemove(function(e) {
  var mousex = e.pageX + 20; //Get X coodrinates
  var mousey = e.pageY + 20; //Get Y coordinates
  var tipWidth = tip.width(); //Find width of tooltip
  var tipHeight = tip.height(); //Find height of tooltip
  
  //Distance of element from the right edge of viewport
  var tipVisX = $(window).width() - (mousex + tipWidth);
  //Distance of element from the bottom of viewport
  var tipVisY = $(window).height() - (mousey + tipHeight);
    
  if ( tipVisX < 20 ) { //If tooltip exceeds the X coordinate of viewport
   mousex = e.pageX - tipWidth - 20;
  } if ( tipVisY < 20 ) { //If tooltip exceeds the Y coordinate of viewport
   mousey = e.pageY - tipHeight - 20;
  } 
  tip.css({  top: mousey, left: mousex });
 });
});
body {
 margin: 0; padding: 0;
 font: normal 12px Verdana, Geneva, sans-serif;
 line-height: 1.8em;
 color: #333;
}
 
* {
 outline: none;
}
img {border: none;}

a {color: #d60000; text-decoration: none;}

/*--Tooltip Styles--*/
.tip {
 display: none;
 color: #fff;
 background:#1d1d1d;
 display:none; /*--Hides by default--*/
 padding:10px;
 position:absolute; z-index:1000;
 -webkit-border-radius: 3px;
 -moz-border-radius: 3px;
 border-radius: 3px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<p style="text-align:left;">My First
  <a href="#" class="tip_trigger">Link 
  </a>
    <span class="tip">
    Some text. Works correctly!
  </span>
</p>
<div style="text-align:left;">My Second
  <a href="#" class="tip_trigger">Link</a>
    <div class="tip">
    <table><tr><td>More text. Doesn't work!</td></tr></table>
  </div>
</div>
Thomas Theunen
  • 1,244
  • 9
  • 13