Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing if JQuery tooltip is initialized

I am using a JQuery UI Tooltip along with AJAX to validate a form.

I am using one tooltip for each fields and I am changing the content of this tooltip depending on the error my AJAX return.

For my code to be complete I need to test if the tooltip is already initialize for this field (change content) if not (create the tooltip).

The problem is that I dont know any affective method of checking if the tooltip is initialize or not.

HTML:

<input type="text" id="text1"/>
<input type="text" id="text2"/>

I have tried the following but they all fail to test if the tooltip is already created or not.

JQUERY:

if($("#text1").tooltip() != null) //or $("#text1").tooltip() != 'undefined'
//does'nt work because .tooltip() always return an object.

if(typeof $("#text1").tooltip() != null)//or typeof $("#text1").tooltip() != 'undefined'
//does'nt work always return an object.

if($("#text1").tooltip().hasOwnProperty('option'))//or $("#text1").tooltip().hasOwnProperty('content')
//does'nt work it always return false.

If someone could help me find a way to check if the tooltip exist it'd be really appreciated

Thanks!

like image 460
Sebastien Avatar asked Sep 16 '13 17:09

Sebastien


People also ask

What is tooltip () method in jQuery?

The tooltip () method allows us the implementation of jQuery tooltip. It replaces the native tooltip which is set using the title attribute. There is also an option called content which can be used to specify the content.

How to control tooltips for disabled elements in jQuery UI?

In general, disabled elements do not trigger any DOM events. Therefore, it is not possible to properly control tooltips for disabled elements, since we need to listen to events to determine when to show and hide the tooltip. As a result, jQuery UI does not guarantee any level of support for tooltips attached to disabled elements.

How do I customize tooltips?

Customizable, themeable tooltips, replacing native tooltips. Tooltips can be attached to any element. When you hover the element with your mouse, the title attribute is displayed in a little box next to the element, just like a native tooltip. But as it's not a native tooltip, it can be styled.

What is the use of the content option in tooltip?

It replaces the native tooltip which is set using the title attribute. There is also an option called content which can be used to specify the content. If we specify both, that is the title attribute and content option, the value of the title attribute gets overridden by the value of the content option.


1 Answers

Just check data('ui-tooltip') on your element. See example:

if($element.data('ui-tooltip')) {
    $element.tooltip('destroy');
}
like image 151
ivan.a.bovin Avatar answered Sep 24 '22 22:09

ivan.a.bovin