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

269
Views
Why works with parragraf and not with textArea, (click over the word to get it)?

i want to get the word clicked and why work with paragraf and not with textArea from html?

document.getElementById("wordcount").addEventListener('click', (e) => {
  s = window.getSelection();
  var range = s.getRangeAt(0);
  var node = s.anchorNode;

  while (range.toString().indexOf(' ') != 0) {
    range.setStart(node, (range.startOffset - 1));
  }
  range.setStart(node, range.startOffset + 1);

  do {
    range.setEnd(node, range.endOffset + 1);

  } while (range.toString().indexOf(' ') == -1 && range.toString().trim() != '');

  var str = range.toString().trim();
  alert(str);
});
<textArea id="wordcount" class="clickable">
  Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris rutrum ante nunc.
</textArea>

here works with <p> but i want to use with textArea

Detect which word has been clicked on within a text what is the problem?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Try this solution:

<textarea id="wordcount" class="clickable">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris rutrum ante nunc.
</textarea>

<script>
    const textarea = document.getElementById('wordcount');

    const grabWord = (start, val) => {
        let arr = val.split(' ');
        let sum = 0;
        for (let index in arr) {
            sum += arr[index].length + 1;
            if (sum > start) return arr[index];
        }
    };

    const getClickedWord = (e) => {
        let start = e.currentTarget.selectionStart;
        console.log(grabWord(start, textarea.value));
    };

    textarea.addEventListener('click', getClickedWord);
</script>

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!