Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to clone content of a div to another div

I want to copy the content of a selected div to another div with jquery clone. but I dont want to append it anywhere

what I mean is when we make a clone of a div with jquery (correct me if i am wrong) we have to set its position and it will dynamically create a new division which is displayed.

but I want to get the content of a selected div and copy it to another pre-set div

like image 814
pahnin Avatar asked Oct 23 '10 05:10

pahnin


People also ask

How do you copy the content of a div into another div?

First, select the div element which need to be copy into another div element. Select the target element where div element is copied. Use the append() method to copy the element as its child.

How do I clone a div?

To clone a div and change its id with JavaScript, we can use the cloneNode method on the div. Then we can set the id property of the cloned element to set the ID. to add the div.

What is clone in jQuery?

The clone() is an inbuilt method in jQuery which is used to make a copy of selected elements including its child nodes, text and attributes. Syntax: $(selector).clone(true|false) Parameter: It accepts an optional parameter which could be either true or false specifies that event handler should be copied or not.


2 Answers

var a = $('#selector').html(); var b = $('#selector').html(a); 

not sure I understood you properly but I think thats what you meant :)

like image 91
Val Avatar answered Oct 08 '22 15:10

Val


I don't agree. Clone can save data without applying to the content.

Look here:

http://www.jsfiddle.net/dactivo/FqffM/

var mylayer=$('.hello').clone(); 

Here you can manage the variable "mylayer" as you want, and it's not in the DOM.

like image 28
netadictos Avatar answered Oct 08 '22 14:10

netadictos