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

211
Visualizações
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 Respostas
Responde à pergunta

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 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