DevExtreme jQuery/JS - 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.

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://url/to/an/image" />
  • <div id="tooltipContainer"></div>

The Tooltip can also be hidden when a user clicks outside it. To control this behavior of the Tooltip, use the closeOnOutsideClick option.

JavaScript
HTML
  • $(function() {
  • $("#tooltipContainer").dxTooltip({
  • target: "#image",
  • showEvent: 'dxhoverstart',
  • hideEvent: 'dxhoverend',
  • closeOnOutsideClick: false,
  • contentTemplate: function (contentElement) {
  • contentElement.append(
  • $("<p />").text("Tooltip content")
  • )
  • }
  • });
  • });
  • <img id="image" src="https://url/to/an/image" />
  • <div id="tooltipContainer"></div>

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.

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://url/to/an/image" />
  • <div id="tooltipContainer"></div>
  • <div id="showButton"></div>
  • <div id="hideButton"></div>

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.

JavaScript
  • $(function() {
  • // ...
  • $("#showButton").dxButton({
  • text: "Show the Tooltip",
  • onClick: function () {
  • $("#tooltipContainer").dxTooltip("show", "#newTarget");
  • }
  • });
  • });

With Angular, Vue, or React, use a different technique. Bind the visible property of the Tooltip widget to a component property. After that, change this component property, and the Tooltip will appear or disappear.

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.

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

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
  • const hiddenEventHandler1 = function (e) {
  • // First handler of the "hidden" event
  • };
  •  
  • const hiddenEventHandler2 = function (e) {
  • // Second handler of the "hidden" event
  • };
  •  
  • $("#tooltipContainer").dxTooltip("instance")
  • .on("hidden", hiddenEventHandler1)
  • .on("hidden", hiddenEventHandler2);
See Also