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

110
Visualizações
2 choices of copying multidimentional array data to clipboard in javascript

I would like to be able to have 2 buttons in my admin panel. One to copy my text as "pure text". And one button to copy my text while adding extra html tags( AND

). I do a lot of copy and paste for my work, and this would really help me work a bit faster.

Let's say you have this array:

let array = [{'head': 1, 'body': 'This is the body 1', 'content': 'This is the content 1'},{'head': 2, 'body': 'This is the body 2', 'content': 'This is the content 2'},{'head': 3, 'body': 'This is the body 3', 'content': 'This is the content 3'}]

In this array, I would like to copy /paste the content(from the above array) like this to a text file:

This is the body 1
This is the content 1
 
This is the body 2
This is the content 2
 
This is the body 3
This is the content 3

(The <br> cannot be visible on the text file...it needs to be a blank space that works in a .txt format, could this " " works?)

The second button should also copy the text from the array but surrounds the body and content with the below tags:

<h2>This is the body 1</h2>
<p>This is the content 1</p>
<br>
<h2>This is the body 2</h2>
<p>This is the content 2</p>
<br>
<h2>This is the body 3</h2>
<p>This is the content 3</p>

At the moment, I am here:

function copyToClipboard(){

    let array = [{'head': 1, 'body': 'This is the body 1', 'content': 'This is the content 1'},{'head': 2, 'body': 'This is the body 2', 'content': 'This is the content 2'},{'head': 3, 'body': 'This is the body 3', 'content': 'This is the content 3'}]
    
    //Copy(clipboard) Text only
            var text  = "";    
            //For Text only
            for (let i = 0; i < array.length; i++) {
              text += array[i]['body'] + "&nbsp;" + array[i]['content'] + "&nbsp;";
            }

    //Copy(clipboard) Text with html tags
    
        for (let i = 0; i < array.length; i++) {
          text += "<h2>" + array[i]['body'] + "</h2>" + "<p>" + array[i]['content'] + "</p>";
        }
    
    
        navigator.clipboard.writeText(text).then(() => {
        alert("Copied to clipboard");
        console.log("Copied to clipboard");
    })
}

I am not sure if I am going in the right direction.

Thanks for your help

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

0

You will need to have 2 functions one for each button.

let array = [{'head': 1, 'body': 'This is the body 1', 'content': 'This is the content 1'},{'head': 2, 'body': 'This is the body 2', 'content': 'This is the content 2'},{'head': 3, 'body': 'This is the body 3', 'content': 'This is the content 3'}]

function copyAsText(){
    //Copy(clipboard) Text only
    var text  = "";    
    //For Text only
    for (let i = 0; i < array.length; i++) {
        text += array[i]['body'] + "\n" + array[i]['content'] + "\n";
    }

    navigator.clipboard.writeText(text).then(() => {
        console.log("Copied to clipboard");
    })
}


function copyAsHTML(){
    var text = "";
    //Copy(clipboard) Text with html tags
    for (let i = 0; i < array.length; i++) {
        text += "<h2>" + array[i]['body'] + "</h2>" + "<br>" + "<p>" + array[i]['content'] + "</p><br>";
    }
 
    navigator.clipboard.writeText(text).then(() => {
        console.log("Copied to clipboard");
    })
}
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