Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

118
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!