8

I need basic idea of creating a custom tooltip using pure Javascript code;

What I want :

For example :

<a href="#" onmousemover="tooltip('text')">Link Text</a>

And onmouseover I want to display a custom tooltip with fixed position based on <a>'s element position, to start from right:0 or from left:0 of the <a> element;

5
  • 8
    Why so many what have you tried these days, where would these people go for help who are beginners ? Hate those words ! Commented Jun 10, 2012 at 19:24
  • @DavidThomas the only problem I had, was with position of created tooltip, I thought I did it right, but the position was not as I expected it to be Commented Jun 10, 2012 at 19:24
  • 2
    @Sarfraz: I have no objection to helping someone who's tried nothing, but, if something's been attempted, I'd rather start with that foreknowledge. While I accept that the opening question may appear to be somewhat over-aggressive, the more-important parts are the subsequent questions, though they build off the foundations of the first. Commented Jun 10, 2012 at 19:26
  • 1
    @DavidThomas: That makes sense then and I totally agree with you in that sense :) Commented Jun 10, 2012 at 19:29
  • I don't know whether you're still around or whether you have found something suited in the meantime, but I created just that last week. It is posted as answer on stackoverflow.com/questions/24458970/…. Commented Jun 29, 2014 at 3:42

3 Answers 3

8

HTML

<a href="https://www.google.com" tooltip="Go to Google">Google</a>

JavaScript

(function () {

var a = document.getElementsByTagName('*'),
    tip, text,
    base = document.createElement('tooltip'); //Defining all objects

for (var x=0;x < a.length;x++) { //I'm using "for" loop to get all "a" elements with attribute "tooltip".
     a[x].onmouseover = function () {
         text = this.getAttribute('tooltip');
         tip = document.createTextNode(text);
         if (text != null) {// Checking if tooltip is empty or not.
               base.innerHTML = '';
               base.appendChild(tip);
               if (document.getElementsByTagName('tooltip')[0]){// Checking for any "tooltip" element
                   document.getElementsByTagName('tooltip')[0].remove();// Removing old tooltip
               }
               base.style.top = (event.pageY + 20) + 'px';
               base.style.left = (event.pageX + 20) + 'px';
               document.body.appendChild(base);
         }

     };
     a[x].onmouseout = function () {
         document.getElementsByTagName('tooltip')[0].remove();// Remove last tooltip
     };
}

})();

By including this script into your page, you just have to use tooltip="Text" in any HTML Element

CSS

tooltip {
    position: fixed;
    background: #fff;
    color: #333;
    border: 2px solid #333;
    font-size: 15px;
}

You can edit CSS as your wish!

Here's my JSFiddle

Sign up to request clarification or add additional context in comments.

Comments

3

As this question comes up when a user searches for custom tooltip, I would like to add the bootstrap's JS tooltip, although you have mentioned pure javascript. So in a sense this answer is only to give the future readers another nice option to work with.

Take a look here for how to use bootstrap tooltip plugin. And here to learn about the options and methods available at your disposal when using Bootstrap's JS Tooltip.

Still I am giving the tryit from their site here:

$(document).ready(function() {
  $('[data-toggle="tooltip"]').tooltip();
});
/* Tooltip */

.test+.tooltip>.tooltip-inner {
  background-color: #73AD21;
  color: #FFFFFF;
  border: 1px solid green;
  padding: 15px;
  font-size: 20px;
}


/* Tooltip on top */

.test+.tooltip.top>.tooltip-arrow {
  border-top: 5px solid green;
}


/* Tooltip on bottom */

.test+.tooltip.bottom>.tooltip-arrow {
  border-bottom: 5px solid blue;
}


/* Tooltip on left */

.test+.tooltip.left>.tooltip-arrow {
  border-left: 5px solid red;
}


/* Tooltip on right */

.test+.tooltip.right>.tooltip-arrow {
  border-right: 5px solid black;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>

<body>

  <div class="container">
    <h3>Tooltip CSS Example</h3>
    <ul class="list-inline">
      <li><a class="test" href="#" data-toggle="tooltip" data-placement="top" title="Hooray!">Top</a></li>
      <li><a class="test" href="#" data-toggle="tooltip" data-placement="bottom" title="Hooray!">Bottom</a></li>
      <li><a class="test" href="#" data-toggle="tooltip" data-placement="left" title="Hooray!">Left</a></li>
      <li><a class="test" href="#" data-toggle="tooltip" data-placement="right" title="Hooray!">Right</a></li>
    </ul>
  </div>
</body>

</html>

2 Comments

Despite your intention, it still doesn't answer the question and doesn't belong here.
@Rob On one hand you are right, it doesn't belong here. But on the other hand, considering the P.S. in the answer, I think it sort of does belong here. Note the sort of, again. :-)
1

You need a function something like this:

function tooltip(text, element) {

    var offsetDistance = 20;

    tt = document.getElementById('ttdiv');
    elem = document.getElementById(element.id);
    tt.style.top = elem.offsetTop + offsetDistance + 'px';
    tt.style.left = elem.offsetLeft + offsetDistance + 'px';
    tt.style.display = 'block';
    tt.innerHTML = text;
}

Where ttdiv is the id of the tooltip div, that is initially set to display:none. You'll need an accompanying hide() function to hide the tooltip when onmouseout is triggered.

Here's a working JSFiddle.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.