Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

90
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda