How do you copy the value of one DIV to another DIV with Javascript?

Asked

Viewed 1,956 times

0

My problem is this:

I have an html page, and on this page, there are 2 Divs. in the second div, I have a javascript that stores a value in a certain variable.

What I want is to display the value of this variable in the first div.

You know how I can do that?

  • 1

    You can put in the code you have so we can understand the problem better?

  • "div, I have a javascript that stores a value in a certain variable." - This is very badly explained. div cannot contain javascript.

  • 2

    It’s poorly explained but from what I understand it’s like taking the contents of the second div to first div, if it is, it’s this next code $("#div1").html($("#div2").html()), warning that it is Jquery

1 answer

2

To create in Pure Javascript just follow this example:

// Elemento com o Texto
var elemento = document.getElementById('teste').innerHTML;
// Escrevendo em outro Elemento
var texto = document.getElementById('texto');
texto.innerHTML = "Texto Copiado: " + elemento;
<div id="teste">
  Exemplo
</div>
<div id="texto"></div>

But if it is in jQuery just follow the example of @Adriano-Resende:

// Elemento com o Texto
var elemento = $('#teste').html();
// Escrevendo em outro Elemento
var texto = $('#texto');
texto.html('Texto Copiado: ' + elemento);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="teste">
  Texto
</div>
<div id="texto"></div>

Browser other questions tagged

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