Lesson 60/100

Tutorials jQuery Tutorial

Dynamic List Rendering

Dynamic List Rendering: free step-by-step lesson with examples, common mistakes, and interview tips — part of jQuery Tutorial on Toolliyo Academy.

On this page

jQuery Tutorial · Lesson 60 of 100

Dynamic List Rendering

Setup & DOM ✓Events Effects AJAX ✓Perf & IntegrateShip & Projects

Perf & Integrate · 3 — Harden · ~10 min · Traversal and Plugins

What is this?

Render lists by emptying a container and appending nodes built with $("

  • ").text(...). Prefer text over html. Delegate events on the parent once.

  • Why should you care?

    Almost every QueryVerse index page is a dynamic list fed by JSON.

    See it live — copy this example

    Examples include the jQuery 3.7 CDN. Paste into an HTML file or use Run Example to preview.

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <title>QueryVerse</title>
      <style>
        body { font-family: system-ui, sans-serif; margin: 1.25rem; }
        .box { padding: .75rem; border: 1px solid #ccc; border-radius: 8px; margin: .5rem 0; }
        .muted { color: #666; }
        button { margin-right: .35rem; margin-top: .35rem; }
        input, select, textarea { margin: .25rem 0; }
        table { border-collapse: collapse; width: 100%; }
        th, td { border: 1px solid #ddd; padding: .4rem .55rem; text-align: left; }
        .hidden { display: none; }
        .row-odd { background: #f7f7f7; }
        .active { font-weight: 700; }
        .toast { background: #111; color: #fff; padding: .5rem .75rem; border-radius: 6px; }
      </style>
    </head>
    <body>
      <button type="button" id="load">Load comments</button>
      <ul id="list" class="box"></ul>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
    <script>
    $(function () {
      $('#list').on('click', '.pin', function () {
        $(this).closest('li').toggleClass('active');
      });
      $('#load').on('click', function () {
        $.getJSON('https://jsonplaceholder.typicode.com/comments?postId=1')
          .done(function (rows) {
            var $list = $('#list').empty();
            rows.slice(0, 5).forEach(function (c) {
              var $li = $('<li/>').append($('<strong/>').text(c.name + ': ')).append(document.createTextNode(c.body.slice(0, 60) + '…'));
              $li.append(' ').append($('<button type="button" class="pin"/>').text('Pin'));
              $list.append($li);
            });
          })
          .fail(function () {
            $('#list').text('Could not load');
          });
      });
    });
    </script>
    </body>
    </html>

    Run Example »

    Edit the code below and click Run to see the result in Toolliyo’s live editor.

    Code
    Result

    What happened?

    • Build document fragments mentally: empty once, append many.
    • Escape with text/createTextNode.
    • Keep delegation outside the load handler so it is not rebound.

    Practice next

    1. Load comments and pin one.
    2. Reload and confirm pins reset with new nodes.
    3. Slice a different length.
    4. Use a