Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created June 12, 2023 02:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/93ec9dda5e9cb1694bb4c87a181d6aa1 to your computer and use it in GitHub Desktop.
Save code-boxx/93ec9dda5e9cb1694bb4c87a181d6aa1 to your computer and use it in GitHub Desktop.
Javascript Move HTML Elements

JAVASCRIPT MOVE HTML ELEMENTS

https://code-boxx.com/move-html-element-javascript/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<!DOCTYPE html>
<html>
<head>
<title>JS Move Element</title>
<!-- (A) DEMO STYLES -->
<style>
.first { background: #ffddf1; }
.second { background: #ddfff7; }
</style>
</head>
<body>
<!-- (B) HTML CONTAINERS -->
<div class="first">
<p>First container.</p>
<p id="source">Paragraph to move.</p>
</div>
<div class="second" id="target">
<p>Second container.</p>
</div>
<!-- (C) MOVE! -->
<script>
function move () {
// (C1) GET SOURCE & TARGET ELEMENTS
var source = document.getElementById("source"),
target = document.getElementById("target");
// (C2) MOVE!
target.appendChild(source);
}
</script>
<input type="button" value="Move" onclick="move()">
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>JS Move Element</title>
<!-- (A) DEMO STYLES -->
<style>
.first { background: #ffddf1; }
.second { background: #ddfff7; }
</style>
</head>
<body>
<!-- (B) HTML CONTAINERS -->
<div class="first" id="source">
<p>First container.</p>
<p>Some content.</p>
</div>
<div class="second" id="target">
<p>Second container.</p>
</div>
<!-- (C) MOVE! -->
<script>
function move () {
// (C1) GET SOURCE & TARGET ELEMENTS
var source = document.getElementById("source"),
target = document.getElementById("target");
// (C2) MOVE!
target.innerHTML += source.innerHTML;
source.innerHTML = "";
}
</script>
<input type="button" value="Move" onclick="move()">
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>JS Move Element</title>
<!-- (A) DEMO STYLES -->
<style>
#clist { display: flex; }
#first, #second { width: 50%; }
.first { background: #ffddf1; }
.second { background: #ddfff7; }
</style>
</head>
<body>
<!-- (B) HTML CONTAINERS -->
<div id="clist">
<div class="first" id="first">
<p>One</p>
<p>Two</p>
<p>Three</p>
</div>
<div class="second" id="second"></div>
</div>
<!-- (C) JAVASCRIPT -->
<script>
window.addEventListener("load", () => {
// (C1) GET ALL HTML ELEMENTS
var first = document.getElementById("first"),
second = document.getElementById("second"),
all = document.querySelectorAll("#first p");
// (C2) TOGGLE CONTAINER
for (let p of all) {
p.onclick = function () {
if (this.parentElement == first) { second.appendChild(this); }
else { first.appendChild(this); }
};
}
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment