DevExtreme Angular - Show and Hide the Tooltip

User Interaction

To specify when the Tooltip should be shown and hidden, set the showEvent and hideEvent options. These options can accept several events at once as well as an object.

jQuery
JavaScript
HTML
$(function() {
    $("#tooltipContainer").dxTooltip({
        target: "#image",
        showEvent: 'dxhoverstart',
        hideEvent: 'dxhoverend',
        contentTemplate: function (contentElement) {
            contentElement.append(
                $("<p />").text("Tooltip content")
            )
        }
    });
});
<img id="image" src="https://www.devexpress.com/DXR.axd?r=9999_17-FD0Id" />
<div id="tooltipContainer"></div>
Angular
HTML
TypeScript
<img id="image" src="https://www.devexpress.com/DXR.axd?r=9999_17-FD0Id" />
<dx-tooltip
    target="#image"
    showEvent="dxhoverstart"
    hideEvent="dxhoverend">
    <div *dxTemplate="let data of 'content'">
        <p>Tooltip content</p>
    </div>
</dx-tooltip>
import { DxTooltipModule } from 'devextreme-angular';
// ...
export class AppComponent {
    // ...
}
@NgModule({
    imports: [
        // ...
        DxTooltipModule
    ],
    // ...
})
ASP.NET MVC Controls
Razor C#
@(Html.DevExtreme().Tooltip()
    .Target("#image")
    .ShowEvent("dxhoverstart")
    .HideEvent("dxhoverend")
    .ContentTemplate(@<text>
        <p>Tooltip content</p>
    </text>)
)
<img id="image" src="https://www.devexpress.com/DXR.axd?r=9999_17-FD0Id" />

The Tooltip can also be hidden when a user clicks outside it or presses the Back button on the device. To control this behavior of the Tooltip, use the closeOnBackButton and closeOnOutsideClick options.

jQuery
JavaScript
HTML
$(function() {
    $("#tooltipContainer").dxTooltip({
        target: "#image",
        showEvent: 'dxhoverstart',
        hideEvent: 'dxhoverend',
        closeOnBackButton: false,
        closeOnOutsideClick: false,
        contentTemplate: function (contentElement) {
            contentElement.append(
                $("<p />").text("Tooltip content")
            )
        }
    });
});
<img id="image" src="https://www.devexpress.com/DXR.axd?r=9999_17-FD0Id" />
<div id="tooltipContainer"></div>
Angular
HTML
TypeScript
<img id="image" src="https://www.devexpress.com/DXR.axd?r=9999_17-FD0Id" />
<dx-tooltip
    target="#image"
    showEvent="dxhoverstart"
    hideEvent="dxhoverend"
    [closeOnBackButton]="false"
    [closeOnOutsideClick]="false">
    <div *dxTemplate="let data of 'content'">
        <p>Tooltip content</p>
    </div>
</dx-tooltip>
import { DxTooltipModule } from 'devextreme-angular';
// ...
export class AppComponent {
    // ...
}
@NgModule({
    imports: [
        // ...
        DxTooltipModule
    ],
    // ...
})
ASP.NET MVC Controls
Razor C#
@(Html.DevExtreme().Tooltip()
    .Target("#image")
    .ShowEvent("dxhoverstart")
    .HideEvent("dxhoverend")
    .ContentTemplate(@<text>
        <p>Tooltip content</p>
    </text>)
    .CloseOnOutsideClick(false)
    .CloseOnBackButton(false)
)
<img id="image" src="https://www.devexpress.com/DXR.axd?r=9999_17-FD0Id" />

API

NOTE
In this article, the Button widget is used to demonstrate how to show and hide the Tooltip. This choice is made for purely demonstrational purposes, and you can do the same operations using another widget following the same guidelines.

To show or hide the Tooltip programmatically, call the show() or hide() method. The same thing can be done using the toggle(showing) method. Pass true or false to this method to show or hide the Tooltip, respectively.

jQuery
JavaScript
HTML
$(function() {
    $("#tooltipContainer").dxTooltip({
        target: "#image",
        contentTemplate: function (contentElement) {
            contentElement.append(
                $("<p />").text("Tooltip content")
            )
        }
    });

    $("#showButton").dxButton({
        text: "Show the Tooltip", 
        onClick: function () {
            $("#tooltipContainer").dxTooltip("show");
            // === or ===
            $("#tooltipContainer").dxTooltip("toggle", true);
        } 
    });

    $("#hideButton").dxButton({
        text: "Hide the Tooltip", 
        onClick: function () {
            $("#tooltipContainer").dxTooltip("hide");
            // === or ===
            $("#tooltipContainer").dxTooltip("toggle", false);
        } 
    });
});
<img id="image" src="https://www.devexpress.com/DXR.axd?r=9999_17-FD0Id" />
<div id="tooltipContainer"></div>
<div id="showButton"></div>
<div id="hideButton"></div>
ASP.NET MVC Controls
Razor C#
@(Html.DevExtreme().Tooltip()
    .ID("tooltip")
    .Target("#image")
    .ContentTemplate(@<text>
        <p>Tooltip content</p>
    </text>)
)
<img id="image" src="https://www.devexpress.com/DXR.axd?r=9999_17-FD0Id" />

@(Html.DevExtreme().Button()
    .ID("showButton")
    .Text("Show the Tooltip")
    .OnClick("showTooltip")
)

@(Html.DevExtreme().Button()
    .ID("hideButton")
    .Text("Hide the Tooltip")
    .OnClick("hideTooltip")
)

<script type="text/javascript">
    function showTooltip () {
        $("#tooltip").dxTooltip("show");
        // ===== or =====
        $("#tooltip").dxTooltip("toggle", true);
    }

    function hideTooltip () {
        $("#tooltip").dxTooltip("hide");
        // ===== or =====
        $("#tooltip").dxTooltip("toggle", false);
    } 
</script>

The show() method called without arguments shows the Tooltip for the target specified beforehand. If you need to change the target once, call the show(target) method.

jQuery
JavaScript
$(function() {
    // ...
    $("#showButton").dxButton({
        text: "Show the Tooltip", 
        onClick: function () {
            $("#tooltipContainer").dxTooltip("show", "#newTarget");
        } 
    });
});
ASP.NET MVC Controls
Razor C#
// ...

<script type="text/javascript">
    function showTooltip () {
        $("#tooltip").dxTooltip("show", "#newTarget");
    }
    // ...
</script>

With Angular, AngularJS, or Knockout, use a different technique. Bind the visible property of the Tooltip widget to a component property (in Angular), a scope property (in AngularJS) or an observable variable (in Knockout). After that, change them, and the Tooltip will appear or disappear.

Angular
HTML
TypeScript
<img id="image" src="https://www.devexpress.com/DXR.axd?r=9999_17-FD0Id" />
<dx-tooltip
    target="#image"
    [visible]="isTooltipVisible">
    <div *dxTemplate="let data of 'content'">
        <p>Tooltip content</p>
    </div>
</dx-tooltip>
<dx-button
    text="Show the Tooltip"
    (onClick)="isTooltipVisible = true">
</dx-button>
<dx-button
    text="Hide the Tooltip"
    (onClick)="isTooltipVisible = false">
</dx-button>
import { DxTooltipModule, DxButtonModule } from 'devextreme-angular';
// ...
export class AppComponent {
    isTooltipVisible: boolean = false;
}
@NgModule({
    imports: [
        // ...
        DxTooltipModule
    ],
    // ...
})
AngularJS
HTML
JavaScript
<div ng-controller="DemoController">
    <div dx-tooltip="{
        target: '#image',
        bindingOptions: {
            visible: 'isTooltipVisible'
        }
    }">
        <p>Tooltip content</p>
    </div>
    <div dx-button="{
        text: 'Show the Tooltip',
        onClick: showTooltip
    }"></div>
    <div dx-button="{
        text: 'Hide the Tooltip',
        onClick: hideTooltip
    }"></div>
</div>
angular.module('DemoApp', ['dx'])
    .controller('DemoController', function DemoController($scope) {
        $scope.isTooltipVisible = false;
        $scope.showTooltip = function () {
            $scope.isTooltipVisible = true;
        };
        $scope.hideTooltip = function () {
            $scope.isTooltipVisible = false;
        }
    });
Knockout
HTML
JavaScript
<div data-bind="dxTooltip: {
    target: '#image',
    visible: isTooltipVisible
}">
    <p>Tooltip content</p>
</div>
<div data-bind="dxButton: {
    text: 'Show the Tooltip',
    onClick: function (e) {
        e.model.isTooltipVisible(true);
    }
}"></div>
<div data-bind="dxButton: {
    text: 'Hide the Tooltip',
    onClick: function (e) {
        e.model.isTooltipVisible(false);
    }
}"></div>
var viewModel = {
    isTooltipVisible: ko.observable(false)
};

ko.applyBindings(viewModel);

Events

To execute certain commands before or after the Tooltip was shown/hidden, handle the showing, shown, hiding or hidden event. If the event handling function is not going to be changed during the lifetime of the widget, assign it to the corresponding onEventName option when you configure the widget.

jQuery
JavaScript
$(function () {
    $("#tooltipContainer").dxTooltip({
        // ...
        onShowing: function (e) {
            // Handler of the "showing" event
        },
        onShown: function (e) {
            // Handler of the "shown" event
        },
        onHiding: function (e) {
            // Handler of the "hiding" event
        },
        onHidden: function (e) {
            // Handler of the "hidden" event
        }
    });
});
Angular
HTML
TypeScript
<dx-tooltip ...
    (onShowing)="onShowing($event)"
    (onShown)="onShown($event)"
    (onHiding)="onHiding($event)"
    (onHidden)="onHidden($event)">
</dx-tooltip>
import { DxTooltipModule } from 'devextreme-angular';
// ...
export class AppComponent {
    onShowing (e) {
        // Handler of the "showing" event
    },
    onShown (e) {
        // Handler of the "shown" event
    },
    onHiding (e) {
        // Handler of the "hiding" event
    },
    onHidden (e) {
        // Handler of the "hidden" event
    }
}
@NgModule({
    imports: [
        // ...
        DxTooltipModule
    ],
    // ...
})
ASP.NET MVC Controls
Razor C#
@(Html.DevExtreme().Tooltip()
    // ...
    .OnShowing("tooltip_onShowing")
    .OnShown("tooltip_onShown")
    .OnHiding("tooltip_onHiding")
    .OnHidden("tooltip_onHidden")
)

<script type="text/javascript">
    function tooltip_onShowing (e) {
        // Handler of the "showing" event
    },
    function tooltip_onShown (e) {
        // Handler of the "shown" event
    },
    function tooltip_onHiding (e) {
        // Handler of the "hiding" event
    },
    function tooltip_onHidden (e) {
        // Handler of the "hidden" event
    }
</script>

If you are going to change event handlers at runtime, or if you need to attach several handlers to a single event, subscribe to the events using the on(eventName, eventHandler) method. This approach is more typical of jQuery.

JavaScript
var hiddenEventHandler1 = function (e) {
    // First handler of the "hidden" event
};

var hiddenEventHandler2 = function (e) {
    // Second handler of the "hidden" event
};

$("#tooltipContainer").dxTooltip("instance")
    .on('hidden', hiddenEventHandler1)
    .on('hidden', hiddenEventHandler2);
See Also