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

320
Visualizações
(select) event doesn't work for div elements

The user can type a text in a div that has the attribute contenteditable="true" and they can also select a text. I now would like to output the selected text.

This is what I have tried:

<div (select)="getSelectedText()" contenteditable="true" #editor id="editor"></div>
getSelectedText(): void {
  console.log(window.getSelection());
}

This is not working, probably because (select) is not available for div. This is what the documentation of it says:

Element: select event

The select event fires when some text has been selected.

...

The event is not available for all elements in all languages. For example, in HTML, select events can be dispatched only on form <input type="text"> and <textarea> elements.

Is there an alternative to this in Angular?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

There is not much you can do but to listen either mouse, either to keyup event:

HTML

<div (keyup)="getSelectedText()" (mouseup)="getSelectedText()" contenteditable="true" #editor id="editor"></div>

TS

  getSelectedText(){
    console.log(document.getSelection()?.toString())
  }

Demo

about 4 years ago · Juan Pablo Isaza Relatório

0

You should use a mouseup and mouseout event in your div and then call window.getSelection().toString() to get the selected text.

<div (mouseup)="getSelectedText()" (mouseout)="getSelectedText()"contenteditable="true" #editor id="editor"></div>

getSelectedText(): void {
   if (window.getSelection) {
        window.getSelection().toString();
    }
}

Ref : Get the Highlighted/Selected text

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