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

182
Visualizações
How to add focus on td on edit button click after setting contentEditableTrue

I am making td editable on click of edit button using contentEditable attribute, but focus is not happening

<tbody>
            {props.tableData && props.tableData.map((row, ind) => {
                return (
                    <tr key={row.id}>
                        <td>{row.id}</td>
                        {/* <td {...editObj}>{row.title}</td> */}
                        <td contentEditable={contentEditableId === row.id ? true : false} suppressContentEditableWarning={contentEditableId === row.id ? true : false}
                        >{row.title}</td>
                        <td style={{ "paddingRight": "20px" }} onClick={(e) => { editTd(e, row.id) }}>Edit</td>
                        <td onClick={() => props.deleteRowData(row.id)}>Delete</td>

                    </tr>)
            })}
        </tbody>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You will need to use refs (useRef hook) & then simply inputRef.current.focus();. I have made a codesandbox for <p>, you can replicate for <td>. Focus happens on load, can use the same approach for a button click etc -

import React, { useRef, useEffect } from "react";
import "./styles.css";

export default function App() {
  const inputRef = useRef(null);
  useEffect(() => {
    inputRef.current.focus();
    // move caret to end
    const textLength = inputRef.current.innerText.length;
    const range = document.createRange();
    const sel = window.getSelection();

    range.setStart(inputRef.current.childNodes[0], textLength)
    range.collapse(true)
    
    sel.removeAllRanges()
    sel.addRange(range)

  }, [inputRef]);
  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
      <div className="create-post">
        <p
          contentEditable
          spellCheck="true"
          placeholder="Write a new post here"
          id="txtContent"
          ref={inputRef}
        >
          Content Editable
        </p>
      </div>
    </div>
  );
}
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