4

I want to use jQuery UI's tooltip feature, however I need it so when you click an element (in my case an image) the tool tip stays open. Can this be done? I couldn't see any options for this.

http://api.jqueryui.com/tooltip/

UPDATE here is my code. I thought the 4th line should work but sadly not:

HTML

<img class="jqToolTip" src="/query.gif" title="Text for tool tip here">

Javascript

$('.jqToolTip').tooltip({
    disabled: false    
}).click(function(){    
    $(this).tooltip( "open" );
//  alert('click');
}).hover(function(){
    // alert('mouse in');
}, function(){
    // alert('mouse out');
});
1
  • is the tooltip set on the image Commented Mar 27, 2013 at 17:08

2 Answers 2

2

I was trying to solve the same exact problem, and I couldn't find the answer anywhere. I finally came up with a solution that works after 4+ hours of searching and experimenting.

What I did was this:

  1. Stopped propagation right away if the state was clicked
  2. Added a click handler to track the state

//This is a naive solution that only handles one tooltip at a time
//You should really move clicked as a data attribute of the element in question
var clicked;
var tooltips = $('a[title]').on('mouseleave focusout mouseover focusin', function(event) {
  if (clicked) {
    event.stopImmediatePropagation();
  }
}).tooltip().click(function() {
  var $this = $(this);
  var isOpen = $this.data('tooltip');
  var method = isOpen ? 'close' : 'open';
  $this.tooltip(method);
  //verbosity for clarity sake, yes you could just use !isOpen or clicked = (method === 'open')
  if (method === 'open') {
    clicked = true;
  } else {
    clicked = false;
  }
  $this.data('tooltip', !isOpen);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/jquery-ui.min.js"></script>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/themes/redmond/jquery-ui.css" rel="stylesheet" />
<a href="#" title="That's what this widget is">Tooltips</a>

Hopefully this will help a future googler.

Thanks in part to this post

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

Comments

0

http://api.jqueryui.com/tooltip/#method-open

$('img.my-class').click(function() {
    $(this).tooltip( "open" );
}

5 Comments

it works only of the clicked element and the element with tooltip are different. Else it won't work
FYI, since you haven't provided any code in your question, it's unreasonable to expect a specific answer. You may need to update selectors to get this to work in your case.
FYI I'm not the guy who asked the question
If you click on the anchor element, it won't stay open
Ive updated my question with my attempt at using your code but its not worked. Thanks

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.