skip to Main Content

I practice Ajax using jQuery.

I am stack now to figure out how to output json data with jQuery for-loop. How can I output tags and even wrap tags inside of for-loop?

HTML tags come out like this:

<main>
  <h1>Ajax</h1>
  <div id="output">
    <h2>Title</h2>
    <h2>Title</h2>
    <div class="block"></div>
    <div class="wrap">
      <div class="card"></div>
      <h3>Card Title</h3>
      <p><img src="https://picsum.photos/200/100" alt="Alt"></p>
      <p>Text</p>
      <h3>Card Title</h3>
      <p><img src="https://picsum.photos/200/100" alt="Alt"></p>
      <p>Text</p>
      <h3>Card Title</h3>
      <p><img src="https://picsum.photos/200/100" alt="Alt"></p>
      <p>Text</p>
    </div>
    <div class="block"></div>
    <div class="wrap">
      <div class="card"></div>
      <h3>Card Title</h3>
      <p><img src="https://picsum.photos/200/100" alt="Alt"></p>
      <p>Text</p>
      <h3>Card Title</h3>
      <p><img src="https://picsum.photos/200/100" alt="Alt"></p>
      <p>Text</p>
      <h3>Card Title</h3>
      <p><img src="https://picsum.photos/200/100" alt="Alt"></p>
      <p>Text</p>
    </div>
  </div><!-- /#output -->
</main>

But I’d like to wrap block, card div tags to contain h3 .etc tags in those.
Ideally I want html to be like this:

<main>
  <h1>Ajax</h1>
  <div id="output">
    <div class="block">
      <h2>Title</h2>
      <div class="wrap">
        <div class="card">
          <h3>Card Title</h3>
          <p><img src="https://picsum.photos/200/100" alt="Alt"></p>
          <p>Text</p>
        </div>
        <div class="card">
          <h3>Card Title</h3>
          <p><img src="https://picsum.photos/200/100" alt="Alt"></p>
          <p>Text</p>
        </div>
        <div class="card">
          <h3>Card Title</h3>
          <p><img src="https://picsum.photos/200/100" alt="Alt"></p>
          <p>Text</p>
        </div>
      </div>
    </div>
    <div class="block">
      <h2>Title</h2>
      <div class="wrap">
        <div class="card">
          <h3>Card Title</h3>
          <p><img src="https://picsum.photos/200/100" alt="Alt"></p>
          <p>Text</p>
        </div>
        <div class="card">
          <h3>Card Title</h3>
          <p><img src="https://picsum.photos/200/100" alt="Alt"></p>
          <p>Text</p>
        </div>
        <div class="card">
          <h3>Card Title</h3>
          <p><img src="https://picsum.photos/200/100" alt="Alt"></p>
          <p>Text</p>
        </div>
      </div>
    </div>
  </div><!-- /#output -->
</main>

Please help.
Thank you in advance.

// $.getJSON("data.json", function(data) {
var data = [{
    "title": "Title",
    "content": [
        { "card": "Card Title", "text": "Text", "img": "https://picsum.photos/200/100", "alt": "Alt" },
        { "card": "Card Title", "text": "Text", "img": "https://picsum.photos/200/100", "alt": "Alt" },
        { "card": "Card Title", "text": "Text", "img": "https://picsum.photos/200/100", "alt": "Alt" }
    ]
},
{
    "title": "Title",
    "content": [
        { "card": "Card Title", "text": "Text", "img": "https://picsum.photos/200/100", "alt": "Alt" },
        { "card": "Card Title", "text": "Text", "img": "https://picsum.photos/200/100", "alt": "Alt" },
        { "card": "Card Title", "text": "Text", "img": "https://picsum.photos/200/100", "alt": "Alt" }
    ]
}];

for (var i in data) {
  const output = $('#output');
  const block = $('<div class="block"></div>');
  const wrap = $('<div class="wrap"></div>');
  const card = $(wrap).append('<div class="card"></div>');

  $(output).append(block).prepend('<h2>' + data[i].title + '</h2>').append(wrap);

  for (var j in data[i].content) {
    $(card).append('<h3>' + data[i].content[j].card + '</h3>')
      .append('<p><img src="' + data[i].content[j].img + '" alt="' + data[i].content[j].alt + '">' + '</p>')
      .append('<p>' + data[i].content[j].text + '</p>');
  }
}
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

#output {
  text-align: center;
}

.block {
  margin: 50px auto;
}

.wrap {
  padding: 20px;
  display: flex;
  justify-content: space-around;
  background-color: #f8f8f8;
}

.card {
  padding: 30px 20px;
  background-color: lavenderblush;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<main>
  <h1>Ajax</h1>
  <div id="output">
  </div>
</main>

2

Answers


  1. Just some issues with your order of appending. Check the corrections:

    // $.getJSON("data.json", function(data) {
    var data = [{
        "title": "Title",
        "content": [
            { "card": "Card Title", "text": "Text", "img": "https://picsum.photos/200/100", "alt": "Alt" },
            { "card": "Card Title", "text": "Text", "img": "https://picsum.photos/200/100", "alt": "Alt" },
            { "card": "Card Title", "text": "Text", "img": "https://picsum.photos/200/100", "alt": "Alt" }
        ]
    },
    {
        "title": "Title",
        "content": [
            { "card": "Card Title", "text": "Text", "img": "https://picsum.photos/200/100", "alt": "Alt" },
            { "card": "Card Title", "text": "Text", "img": "https://picsum.photos/200/100", "alt": "Alt" },
            { "card": "Card Title", "text": "Text", "img": "https://picsum.photos/200/100", "alt": "Alt" }
        ]
    }];
    
    for (var i in data) {
      const output = $('#output')
      , block = $('<div class="block"></div>')
      , wrap = $('<div class="wrap"></div>')
      
    
      for (var j in data[i].content) {
        const card = $('<div class="card"></div>');
          card
          .append('<h3>' + data[i].content[j].card + '</h3>')
          .append('<p><img src="' + data[i].content[j].img + '" alt="' + data[i].content[j].alt + '">' + '</p>')
          .append('<p>' + data[i].content[j].text + '</p>');
          wrap.append(card);
      }
          
      
      block.append('<h2>' + data[i].title + '</h2>');
      block.append(wrap);  
      output.append(block);
    }
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }
    
    #output {
      text-align: center;
    }
    
    .block {
      margin: 50px auto;
    }
    
    .wrap {
      padding: 20px;
      display: flex;
      justify-content: space-around;
      background-color: #f8f8f8;
    }
    
    .card {
      padding: 30px 20px;
      background-color: lavenderblush;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <main>
      <h1>Ajax</h1>
      <div id="output">
      </div>
    </main>
    Login or Signup to reply.
  2. You can just change $(card) to $(block) near the end of the script

    // $.getJSON("data.json", function(data) {
    var data = [{
        "title": "Title",
        "content": [
            { "card": "Card Title", "text": "Text", "img": "https://picsum.photos/200/100", "alt": "Alt" },
            { "card": "Card Title", "text": "Text", "img": "https://picsum.photos/200/100", "alt": "Alt" },
            { "card": "Card Title", "text": "Text", "img": "https://picsum.photos/200/100", "alt": "Alt" }
        ]
    },
    {
        "title": "Title",
        "content": [
            { "card": "Card Title", "text": "Text", "img": "https://picsum.photos/200/100", "alt": "Alt" },
            { "card": "Card Title", "text": "Text", "img": "https://picsum.photos/200/100", "alt": "Alt" },
            { "card": "Card Title", "text": "Text", "img": "https://picsum.photos/200/100", "alt": "Alt" }
        ]
    }];
    
    for (var i in data) {
      const output = $('#output')
      , block = $('<div class="block"></div>')
      , wrap = $('<div class="wrap"></div>')
      , card = $('<div class="card"></div>');
    
      for (var j in data[i].content) {
        $(block).append('<h3>' + data[i].content[j].card + '</h3>')
          .append('<p><img src="' + data[i].content[j].img + '" alt="' + data[i].content[j].alt + '">' + '</p>')
          .append('<p>' + data[i].content[j].text + '</p>');
      }
          
      wrap.append(card);
      block.append('<h2>' + data[i].title + '</h2>');
      block.append(wrap);  
      output.append(block);
    }
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }
    
    #output {
      text-align: center;
    }
    
    .block {
      margin: 50px auto;
    }
    
    .wrap {
      padding: 20px;
      display: flex;
      justify-content: space-around;
      background-color: #f8f8f8;
    }
    
    .card {
      padding: 30px 20px;
      background-color: lavenderblush;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <main>
      <h1>Ajax</h1>
      <div id="output">
      </div>
    </main>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search