Skip to content

Instantly share code, notes, and snippets.

@rauschma
Created May 21, 2026 17:33
Show Gist options
  • Select an option

  • Save rauschma/ab041e737b712590f7bc6b4b11cc2a49 to your computer and use it in GitHub Desktop.

Select an option

Save rauschma/ab041e737b712590f7bc6b4b11cc2a49 to your computer and use it in GitHub Desktop.
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Reverse children</title>
</head>
<body>
<div id="container">
<div>Child A</div>
<div>Child B</div>
<div>Child C</div>
<div>Child D</div>
</div>
<button>Reverse</button>
<script type="module">
document.querySelector('button').addEventListener(
'click',
() => {
const container = document.getElementById('container');
const children = Array.from(container.childNodes);
for (const ch of children) {
container.removeChild(ch);
}
children.reverse();
for (const ch of children) {
container.appendChild(ch);
}
}
);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment