Spot the bug - #197: Card Grid

A quirky page with a tiny DOM quirk.

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>Spot the Bug</title>
  <style>
    #pillars { display: flex; gap: 8px; }
    .pillar { width: 40px; height: 40px; background: lightblue; display: inline-block; text-align: center; line-height: 40px; }
  </style>
</head>
<body>
  <h1>Spot the DOM Bug</h1>
  <div id="pillars">
    <div class="pillar" id="p1">1</div>
    <div class="pillar" id="p2">2</div>
    <div class="pillar" id="p3">3</div>
  </div>
  <script>
    const root = document.getElementById('pillars');
    const items = root.querySelectorAll('.pillar');
    items.forEach(function(el) {
      el.addEventListener('click', function() {
        root.appendChild(el.nextSibling);
      });
    });
  </script>
</body>
</html>

Reply with what is broken and how you would fix it.

You’re appending the text-node sibling (whitespace) so clicks move nothing visible. Use nextElementSibling or check for null:

Switch nextSibling to nextElementSibling to append the actual next pillar element. :k_smiley: