Thursday, August 8, 2013

Creating Tooltips in Bootstrap - Two Different Styles

In this post, I am going to go over the two different ways to make tooltips in Xpages using Twitter Bootstrap.

Background

I was wanting to create tooltips to shorten my labels and streamline a crowded window with many input fields in the application I am building.   After a quick search, I found an article that Kathy Brown wrote on how to create tooltips.   Within a few minutes I had a working tooltip, but I wanted tooltips that looked like what I saw on the Bootstrap site.
I wanted my tooltips to look like this
I tried a changing attributes using and couldn't do anything to change the default behavior.  I did remember to add 'data-' before the attributes.   After some searching and the help of jsfiddle, I was able to figure how the easiest way to create tooltips how I wanted them.

Option 1

This way is extremely simple and quick to implement.   In my case, I wanted to the tooltips to be on the labels.  You will notice that I am using a Bootstrap feature where I combine my input field with a label together using what is called an "input-prepend".   The label itself is wrapped in a <span> with a class of "add-on".   I wanted my tooltip to act on the label text in the add-on.   If you want the tooltip to act up on a control such as a button, you will want to add a new attribute (attrs) in All Properties of the control.

  1. Wrap your text in a span if it is not already OR Add a new attribute to control you are using.   In my case I am writing the html directly in the source.
  2. Add a property called "data-toggle" with a value of "tooltip"  See Update below
  3. Add a property called "title" with the text you want in the tooltip
Example:  <span class="add-on" data-toggle="tooltip" title="Seventh Street">02</span>

You will get a result like you see in the screenshot below.  The tooltip shows up in relation to the mouse location, always in the lower right.  You cannot control the location, or the delay.

The result of Option 1
UPDATE:  Upon further testing, I think a correction is in order.  Option 1 is not a bootstrap feature at all, but is part of HTML 5.   All you need is the "title" attribute to make this work.   The "data=toggle" does nothing.   

Option 2 is the Bootstrap way of writing tooltips.

Option 2

This is the way that tooltips are shown on the Bootstrap site, the first option isn't mentioned on the current site.  The first option would be fine and looks decent, but I wanted a little more control over placement and timing.

  1. Add a html property called "title" with the text of the tooltip.  (No need for 'data-toggle' with this option)
  2. If you already have a "class" attribute then add a second one separated by space.  Name it what you want, except don't use "tooltip" or it won't work.  I called mine "tips".  If you don't have a "class" attribute then create one.
  3. Add code similar to the following in your $(document).ready(function(). If you don't already have one that add one to the onClientLoad event of your Xpage, make sure you choose Client!
  4. Customize the tooltips however you want, using the bootstrap site as your guide.  In my case, I made the delay one second, and the placement always at the bottom.  With this option, the tooltip placement is based on the control it is attached to, and not the mouse location within the control.
Example:  <span class="add-on tips" title="West">01</span>
Code to enable the tooltip (Line 2 is unrelated the tooltip)

The result of Option 2

6 comments:

  1. Option 1 was a way with Bootstrap, however it has since been deprecated.

    ReplyDelete
    Replies
    1. That makes sense, they probably figured it wasn't needed since HTML 5 now did the same thing. http://www.w3schools.com/tags/att_global_title.asp

      Delete
  2. This was very helpful, thanks. Also, I found I had to place the javascript below the call to bootstrap.js, i.e. I have this at the bottom just before the closing body tag:
    <`!-- Placed at the end of the document so the pages load faster -->
    <`script src="js/jquery.js"><`/script>
    <`script src="js/bootstrap.min.js"><`/script>
    <`script type="text/javascript">
    $(document).ready(function() {
    $(".tipsB").tooltip({placement: "bottom"});
    });
    <`/script>

    ReplyDelete
  3. bootstrap modal (the frame) is an extended backbone view (its thin layer is defined inside the lib), the backbone modal .... Subscribe to: Post Comments (Atom)bootstrap

    ReplyDelete
  4. I see the Twitter Bootstrap framework being used more and more by developers and I am still struggling to understand how it can be accepted as a full and robust solution that can be released as production ready. I love how subtle the colors are. It looks great!

    ReplyDelete
  5. Thank you for this post because this helped solved some twitter bootstrap tooltip problem

    ReplyDelete