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

164
Visualizações
is it possible to copy html to clipboard for hidden element?

I have a section in which I want user to copy html to clipboard, this element should be hidden here is what I have so far

live demo : live demo

Here is code I have so far.

export default function App() {
  const tableRef = useRef(null);
  const textRef = useRef(null);
  const [copySuccess, setCopySuccess] = useState("");

  const copyToClipboard = (e) => {
    let tableRange = document.createRange();
    tableRange.selectNodeContents(tableRef.current);
    let sel = window.getSelection();
    sel.removeAllRanges();
    sel.addRange(tableRange);
    document.execCommand("Copy");
    sel.removeAllRanges();
    setCopySuccess("Copied!");
  };

  return (
    <div className="App">
      <textarea
        ref={textRef}
        style={{ opacity: 0, position: "absolute", top: "-200px" }}
      ></textarea>
      <div
        aria-hidden="true"
        ref={tableRef}
        className="table"
        style={{ display: "none" }}
      >
        <table>
          <thead>
            <tr>
              <th>#</th>
              <th>First Name</th>
              <th>Last Name</th>
              <th>Username</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td>1</td>
              <td>Mark</td>
              <td>Otto</td>
              <td>@mdo</td>
            </tr>
          </tbody>
        </table>
      </div>

      {document.queryCommandSupported("copy") && (
        <div>
          <button onClick={copyToClipboard}>Copy</button>
          {copySuccess}
        </div>
      )}
    </div>
  );
}

Expected: copy the hidden html table to clipboard

What do I need to do so that I can copy that hidden element to clipboard?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Ok, hidden element is not available directly in DOM so you can not copy it from there. They are just used to carry data from one state to another or from rendering form to back to controller. If you need that data, you can bind that in ViewBag.

const a = window.param;

window.param = ViewBag.param;

const copyToClipboard = (e) => {
ViewBag.param = e;
    let tableRange = document.createRange();
    tableRange.selectNodeContents(tableRef.current);
    let sel = window.getSelection();
    sel.removeAllRanges();
    sel.addRange(tableRange);
    document.execCommand("Copy");
    sel.removeAllRanges();
    setCopySuccess("Copied!");
  };

Please refer this link also. Access a viewbag property in reactjs

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