0

I have 2 diferent divs: [Div1] and [Div2]. My goal is that when i click on some event, i want [Div1] to be the exactly the same as the [Div2]. I used this code:

 document.getElementById("div1")=document.getElementById("div2");

This is a javascript error and i dont know how to do anything like this. I cant copy every element cuz those might change based on the users actions. I found something about cloning a node but i couldn't put it to work. Any sugestions?

Chazz1
  • 133
  • 1
  • 4
  • 16

2 Answers2

0

Use jquery instead; when the user clicks the button/any action, use the following snippet:

$("div1").html($("div2").html());
Vidhya Sagar Reddy
  • 1,521
  • 1
  • 13
  • 25
0

Well depending on what you mean by "exactly the same" (sharing the same reference? duplication of values?), you might want to try cloning:

Try:

var node = document.getElementById("div2");
var node2 = node.cloneNode(true); //creates deep clone with events you can do something with

///Or you could just copy the markup over
document.getElementById("div1").innerHTML = node2.innerHTML;
Mister Epic
  • 16,295
  • 13
  • 76
  • 147
  • its exatly that that im trying but im not getting the same values, im getting both the div1 and div2 values (instead of only the new ones) – Chazz1 Jul 16 '13 at 16:03
  • Not entirely sure what you're after, maybe you can post more code? – Mister Epic Jul 16 '13 at 16:05
  • Try cleaning div1 first: [Remove all child elements of a DOM node in JavaScript?](http://stackoverflow.com/questions/3955229/remove-all-child-elements-of-a-dom-node-in-javascript). – Filipe Silva Jul 16 '13 at 16:11
  • Thanks Filipe that was the mistake. Sorry for such a amateur error and thanks for all the help – Chazz1 Jul 16 '13 at 16:26