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

119
Visualizações
selected text and Highlight with javascript in textarea and div at the same time

selected text and Highlight with javascript in textarea and div at the same time

this demo just shows the selected text in div, but I need to show the same text in both books, just show what text is Highlighted in div or textarea at the same time

<!DOCTYPE html>
<html>

<body>

  <p id="test">This example uses the addEventListener() method to attach a "select" event to an element.</p>

  <input style="width:100%" type="text" value="Only text in the input field is being displayed when selected!" id="myText">
  <p id="demo"></p>
  <script>
    document.addEventListener("mouseup", function(){
    if(document.getSelection()){
      document.querySelector("#demo").textContent = document.getSelection();
    }else if(window.getSelection()){
      document.querySelector("#demo").textContent = document.getSelection();
    }else if(document.selection){
      document.querySelector("#demo").textContent = document.selection.createRange().text;
    }
    });
  </script>

</body>

</html>

chek my screenshot

enter image description here

I just need to show the text what is selected in textarea or div need to change color in both blocks at the same time

also, my textarea don't have id or class - and I want to keep it this way

<textarea></textarea>

thank you - if you can show a javascript example will be very helpful.

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

0

you can select textarea with:

document.querySelector("textarea").style.backgroundColor = "yellow";

window.getSelection() has all the properties to count offset of the selection from the entire 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