0

I want to display an element title on the image(tooltip size). I found out the current element title

$('.downloadPdf').mouseover(function(){
    var currentelement = $(this).attr('title');
});

downloadPdf is common class on elements.

Actually I have a menu bar in bottom in which I have some icons. These icons are functional. Here I want whenever a user hovers over these icons, their title must come over the image. This image is nothing, just a tooltip shape.

But how to append it on image? And its position must be on the element only. so how do I get the position also. So confused and spend at least 3 hours on this small task. Any help would be appreciated. I am so bad when it comes to use css effectively.

David Thomas
  • 249,100
  • 51
  • 377
  • 410
Nitu Dhaka
  • 1,300
  • 3
  • 14
  • 29

4 Answers4

5

Ok. The easiest and simpliest way is to add title attribute to your image. Hovering this image will cause the native browser tooltip to show over the image.

Check this demo (place your mouse cursor over the image of pdf-icon).

Also these questions has already been asked here.

Community
  • 1
  • 1
CmajSmith
  • 432
  • 2
  • 8
  • yes it occurs by default rit? so can we change the design of default tooltip? – Nitu Dhaka Nov 04 '14 at 11:06
  • Simple answer: no. But there is a way to make your own tooltip and show it instead of native one. But this demands changes in your html. I will make an example in 10 mins. – CmajSmith Nov 04 '14 at 11:09
2

Here is an organized demo I made in the last few minutes: http://jsfiddle.net/47g8dnLx/1/

HTML:

<div class="icon">
    <img src="path/to/image">
    <span>Text</span>
</div>

.icon is the wrapper of each icon. span is the tool tip text.

The demo is just a working demo. No mean of code tidiness or efficient!

Please note that I omitted the title="" because it's not needed for custom tool tips.

Community
  • 1
  • 1
Daniel Cheung
  • 4,779
  • 1
  • 30
  • 63
  • +1 Good one Daniel. In fact you could use the `title` as well if required. http://jsfiddle.net/abhitalks/47g8dnLx/2/ – Abhitalks Nov 04 '14 at 11:20
  • @abhitalks Well... :/ Adding title in there just adds more code and the span's content renders useless. _If possible_, putting the title in the `` would be a better alternative. – Daniel Cheung Nov 04 '14 at 11:23
1

Hi i think you want to this modify title

now used to this code

$.fn.autoSuggest = function(){   return this.each(function(index, elm){ 
if(!$(elm).is('[data-title]')){ 
$(elm).attr('data-title', $(elm).attr('title')).attr('title', ''); 
}; 

$(elm).on('mouseenter', function(){ 
var element = $(this), 
posTop = element.offset().top + element.outerHeight() + 10, 
posLeft = element.offset().left, 
toolTipWidth = element.outerWidth() > 90 ? element.outerWidth() : 250, 
titleText = element.attr('data-title'); 
if(titleText && titleText != ''){ 
$('<div />', {class: 'autoSugest', text : titleText, css : {left: posLeft, top: posTop, maxWidth: toolTipWidth}}).appendTo('body').fadeIn(); 
}else{ 
return false; 
} 
}); 
$(elm).on('mouseout', function(){ 
$('.autoSugest').fadeOut(function(){ 
$(this).remove(); 
}); 
}); 
}); }; 
$(document).ready(function(){ 
    $('.title').autoSuggest(); });
.downloadPdf {
    width:64px;
    height:64px
}

.autoSugest{background:#fffdef;border:1px solid #cac6ad; 
-webkit-border-radius:4px; 
-moz-border-radius:4px; 
border-radius:4px;color:#7f7943; 
display:none; 
font-size:12px; 
padding:7px 15px; 
position:absolute; 
min-width:100px; 
-o-box-sizing:border-box; 
-ms-box-sizing:border-box; 
-webkit-box-sizing:border-box; 
-moz-box-sizing:border-box; 
box-sizing:border-box;}

.autoSugest:after{ 
content:"";
 border-left: solid 10px transparent;
border-right: solid 10px transparent;
border-bottom: solid 10px #cac6ad;
position: absolute;
top: -11px;
left: 12px;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<img class="downloadPdf title" src="http://icons.iconarchive.com/icons/graphicloads/filetype/256/pdf-icon.png" title="Text for tooltip">

How to create tooltip

Demo Fiddle

Rohit Azad Malik
  • 31,410
  • 17
  • 69
  • 97
0

I just found it (not done by me).

 $(document).ready(function() {
   $('img').hover(function() {
     // Hover over code
     var title = $(this).attr('alt');
     $(this).data('tipText', title);
     $('<p class="tooltip"></p>').text(title).appendTo('body').fadeIn('slow');
   }, function() {
     // Hover out code
     $(this).attr('alt', $(this).data('tipText'));
     $('.tooltip').remove();
   });
 });
            .tooltip {
                display: none;
                position: absolute;
                border: 1px solid #333;
                background-color: #161616;
                border-radius: 5px;
                padding: 10px;
                color: #fff;
                font-size: 12px Arial;
            }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<img src="https://www.google.com.br/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png" alt="Google Logo" />
ARNON
  • 1,097
  • 1
  • 15
  • 33