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

210
Views
Selecting all text in a table cell on focus in Svelte or JS

This is more of a JS and HTML question than svelte.

I want to select all the text in the field when the element gets focus. It works fine when the element is an input element but it does not work with td. How can I achieve this with td (no jQuery)?

<script>
    function handleFocus(e) {
        e.target.select();
    }
</script>

<input on:focus={handleFocus} value="Testing"/>
<td on:focus={handleFocus} contenteditable="true">Testing</td>

Svelte REPL

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

0

You need to create a selection range.

<script>
function handleFocus(e) {
        e.target.select();
    }
    
    function tdFocus(e) {
        const el = e.target
        const range = document.createRange();
       range.selectNodeContents(el);
        const sel = window.getSelection();
       sel.removeAllRanges();
       sel.addRange(range);
    }
</script>

<input on:focus={handleFocus} value="Testing"/>
<td on:focus={tdFocus} contenteditable="true">Testing</td>

Svelte REPL

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!