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

628
Visualizações
How to copy and paste HTML/text by JavaScript

I want to copy a text that has the CSS class copytext and want to paste on the same page that's has CSS class pastehere.

I am using the following code, but with no result.

function copyToClipboard(text) {
   const elem = document.createElement('.copytext');
   elem.value = text;
   document.body.appendChild(elem);
   elem.select();
   document.execCommand('.pastehere');
   document.body.removeChild(elem);
}

about 4 years ago · Santiago Gelvez
3 Respostas
Responde à pergunta

0

I think you want to get the value of an HTML

tag or something? with DOM Element innerHTML you can get the value of the Tag. eg:

html:

`<p id="myTag">hello world</p>`

javascript:

`let var1 = document.getElementById("myTag").innerHTML;`

the 'hello world' is now stored in the variable 'var1' and you can continue to work with it normally

about 4 years ago · Santiago Gelvez Relatório

0

  1. If you want to copy text from all elements .copytext to the element .pastehere:

HTML:

<div class="copytext">Line 1</div>
<div class="copytext">Line 2</div>
<div class="copytext">Line 3</div>

JS:

function fun(text){
  let copiedText = "";
  text.forEach(element => {
    copiedText += element.textContent + "\n";
  });
  const resault = document.createElement('div');
  resault.classList.add('pastehere');
  resault.innerText = copiedText;
  document.body.appendChild(resault);
}

let copyFrom = document.querySelectorAll('.copytext');
fun(copyFrom);

You'll get:

<div class="pastehere">
    Line 1 <br>
    Line 2 <br>
    Line 3 <br>
</div>
  1. If you want to create element with the text you pass to the function as an argument:

JS:

function fun(text){
  let resault = document.createElement('div');
  resault.classList.add('pastehere');
  resault.innerText = text;
  document.body.appendChild(resault);
}

let text = "My text";
fun(text);

You'll get:

<div class="pastehere">
    My text
</div>

!! Note that you can only type a tag name in document.createElement('');

about 4 years ago · Santiago Gelvez Relatório

0

You can use

document.getElementById("ID NAME")

To copy.

To change you can use

document.getElementsByClassName("class-name").style[0] = "max-width: 10%;"
about 4 years ago · Santiago Gelvez 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