Copy code snippet, modify and paste into another Div

Asked

Viewed 584 times

-1

I am structuring an experimental system where when clicking a button it copies what is selected (in this case a snippet in html) as the below:

<section>
  <h1> titulo </h1>
  <p>texto desta área</p>
</section>

<button>Modificar código</button>

Then he would make a change to the code by inserting the tags that need to be inserted (for my need)

[[bloco]]
    [[repetir]]
    <section>
      <h1> titulo </h1>
      <p>texto desta área</p>
    </section>
    [[/repetir]]
[[/bloco}}

Do I need to use regex? Is there any way to do this just by copying the content, changing and pasting without using regex?

  • Do you already have part of the code ready? It would help if you post; Since you will change "before" and "after" of the copied code, just concatenate the content: textoAntes + textoCopiado + textoDepois.

  • 1

    Please read Why Regex should not be used to handle HTML?. If it is a very specific html structure, Regex can be used. But if it is to generalize to multiple sites, it is not recommended.

  • Document.getelementby..(). hinnerhtml helps you copy, you just have to put an id or class in the tag you want. After that, the "..." replaces with id or classname and us () puts the id or class name.

1 answer

0

A simple way to copy code is by using innerHTML but it would take something outside to catch that bit of code, for example:

<div id="copiarConteudo">
 <section>
  <h1> titulo </h1>
  <p>texto desta área</p>
 </section>
</div>

In this case it was enough:

<button onclick="Copia();">Copiar</button>
<script>
  function Copia(){
    var codigo = document.getElementById("copiarConteudo").innerHTML;
  }
</script>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.