In prototype or normal-but-cross-browser-compatible Javascript, how do I move the contents of a div to the contents of another div?
Inside the div is a form with ids and dependent Javascript code with event observers. I don't want this to break just because I have moved a block in the DOM. A 'this innerHTML = that innerHTML' solution is not what I am looking for. I will also be needing to do this when the DOM is loaded.
I want to go from this:
<div id='here'>
<div id='MacGuffin'>
<p>Hello Worlds!</p>
</div>
</div>
<div id='there'>
</div>
to this:
<div id='here'>
</div>
<div id='there'>
<div id='MacGuffin'>
<p>Hello Worlds!</p>
</div>
</div>
...when the document loads with nothing jumping about.
Answer: Use the jQuery . appendTo() Method You can use the jQuery . appendTo() method to move an element into another element.
To move the inner div container to the centre of the parent div we have to use the margin property of style attribute. We can adjust the space around any HTML element by this margin property just by providing desired values to it.
Answer: Use the jQuery html() Method You can simply use the jQuery html() method to replace innerHTML of a div or any other element.
Basically you can't move div using CSS but you can show and hide div as per viewport. Here is an example, i hope it will help you.
You can put a div inside an div but once you do that you can only put block elements (like divs) inside the first div. And if you put an inline element inside an div only inline elements can go inside the div.
All you have to do is select the element(s) you want to move, then call an “adding” method such as append() , appendTo() or prepend() to add the selected elements to another parent element.
You can add this at the very end of the BODY tag:
<script>
document.getElementById('there').appendChild(
document.getElementById('MacGuffin')
);
</script>
MacGuffin
will be moved automatically from one to the other.
And there won't be any flickering.
Perhaps not a major point, but there is no Node.migrateChild() built-in Javascript interface method. If a fragment already has a parent elsewhere in the DOM (as div id='MacGuffin' does in example markup above), appendChild() quietly detaches the argument fragment from any current parent before reattaching under the new parent. In my mind migrateChild() would be a more semantically meaningful method name than appendChild(), less need for StackOverflow developer searches to allude to its more powerful abilities.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With