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

91
Vistas
Selected text appearing after mouseup disappears with any click

I have written code in my Angular app to get the text snippet selected/highlighted by the user into a text area:

<textarea name="highlightedText" id="selectedText" rows="3" class="form-control"
    placeholder="Your highlighted text will appear here..."
    [(ngModel)]="highlightedText" (mouseup)="mouseUp()">
</textarea>

In my component, I have:

// Content of user annotation
highlightedText: string = "";

constructor() {
    document.body.addEventListener("mouseup", () => {
      this.mouseUp();
    });
}

mouseUp() {
    if (window.getSelection) {
      this.highlightedText = window.getSelection()!.toString();
    }
    return this.highlightedText;
}

This works; meaning the text I select appears in the text box, but as soon as I click in that box (or click anywhere on the page) the text disappears.

What am I doing wrong that the transferred text does not stay there?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

What did You actually expected - the val being reevaluated when You click (making 'mousedown' / 'mouseup') ...

  mouseUp() {
    if (window.getSelection) {
      this.reEval();
    }
    return this.highlightedText;
  }

  reEval() {
    const val = window.getSelection()!.toString().trim();
    if (!!val.length) this.highlightedText = val;
  }
about 4 years ago · Juan Pablo Isaza 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