Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

419
Vistas
How to click on an element that is in the body pragmatically from inside of an angular component?

I have a website on which I have crisp chat service running and I'm trying to click on the chat box from one of my component's typescript files.

The crisp chat box is a div with the id of crisp-client inside body tag. How do I click on it from inside my typescript?

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

You can use the ViewChild decorator and pass a template reference variable as a string, and manipulate the event. Here's a simple example of clicking Button 1, which programmatically clicks Button 2 and due to that click, the text of Button 2 is changed:

<button #button1 (click)="clickButton2()">
  Click to change the text of Button 2
</button>

<button #button2 (click)="changeText()">{{ buttonText }}</button>

buttonText = 'Button 2';
  
  @ViewChild('button2') button2: ElementRef<HTMLElement>;
  clickButton2() {
    let el: HTMLElement = this.button2.nativeElement;
    el.click();
  }

  changeText() {
    this.buttonText="Changed!"
  }

Here's a Stackblitz demo

UPDATE

You can access elements of body using document object in Angular. This way, you can achieve the click of the element which is inside body. Here's one more example where I am clicking the button which is inside body from a component.

<button (click)="handleClick()">Programatically click the button inside body</button>
ngOnInit() {
    let btn = document.getElementById('main');
    btn.addEventListener('click', (e: Event) => btn.innerHTML = "Clicked!");
  }

  handleClick() {
    document.getElementById('main').click();
  }

index.html

<html>
  <body>
    <my-app>loading</my-app>
    <button id="main">Click</button>
  </body>
</html>

Here's a Stackblitz demo.

over 4 years ago · Santiago Trujillo Denunciar

0

Inject Renderer2 in constructor -

constructor(private renderer: Renderer2) {}

Get the crisp-client element in your component -

let crispClient = this.renderer.selectRootElement('#crisp-client');

Perform click() event -

crispClient.click();

Working demo here

Added a test method on div to check -

<div id="crisp-client" onClick="console.log('crisp client clicked')"></div>
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda