Tuesday, 3 May 2016

javascript - How to move an element into another element?



I would like to move one DIV element inside another. For example, I want to move this (including all children):




...




into this:




...




so that I have this:





...



Answer



You may want to use the appendTo function (which adds to the end of the element):




$("#source").appendTo("#destination");


Alternatively you could use the prependTo function (which adds to the beginning of the element):



$("#source").prependTo("#destination");






Example:





$("#appendTo").click(function() {
$("#moveMeIntoMain").appendTo($("#main"));
});
$("#prependTo").click(function() {
$("#moveMeIntoMain").prependTo($("#main"));

});

#main {
border: 2px solid blue;
min-height: 100px;
}

.moveMeIntoMain {
border: 1px solid red;
}



main

move me to main







No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...