Evanss
Evanss

Reputation: 23553

Keep jQuery tooltip open on click?

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');
});

Upvotes: 4

Views: 5326

Answers (2)

mpd
mpd

Reputation: 2230

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

Upvotes: 2

isherwood
isherwood

Reputation: 61056

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

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

Upvotes: 0

Related Questions