1

I am trying to trigger ngx-bootstrap Tooltip from javascript. Below code is working without any problem. Now I want to call show() function from javascript side manually.

Problem is I don't know what "bs-tooltip" is. Is it an id of element created by ngx-bootstrap. If so how can I get this element from javascript?

Ngx-bootstrap Tooltip

<p>
  <span tooltip="Hello there! I was triggered manually"
        triggers="" #customTooltip="bs-tooltip">
  This text has attached tooltip
  </span>
</p>

<button type="button" class="btn btn-success" (click)="customTooltip.show()">
  Show
</button>
<button type="button" class="btn btn-warning" (click)="customTooltip.hide()">
  Hide
</button>
<button type="button" class="btn btn-info" (click)="customTooltip.toggle()">
  Toggle
</button>

EDIT:

document.getElementById("bs-tooltip")  //returns null

1 Answer 1

3

Try to use viewChild to acces the customTooltip in your component's class:

import { ViewChild } from '@angular/core';

export class YourComponent {
    @ViewChild('customTooltip') tooltip: ElementRef;
    onClick() {
        this.tooltip.show();
    }
}

<span #customTooltip="bs-tooltip" tooltip="Hello there! I was triggered manually" triggers="" > This text has attached tooltip</span>
Sign up to request clarification or add additional context in comments.

2 Comments

Your code is working fine but I am getting "Property 'show' does not exist on type 'ElementRef'." error. Is there any way to handle this error?
Try to give a type to the tooltip element: import {TooltipDirective } from 'ngx-bootstrap'; @ViewChild('customTooltip') tooltip: TooltipDirective;

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.