Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9282253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T18:19:36+00:00 2026-06-18T18:19:36+00:00

I am populating divs with json but I don’t think I am doing it

  • 0

I am populating divs with json but I don’t think I am doing it the best way. Seems like alot of code and some it seems like it could share.

//data for DOM
$.getJSON('json/device.json', function (data) {
    var items = [];
    $.each(data[0].attributes, function (key, val) {
        items.push('<li id="' + key + '">' + '  ' + val + '</li>');
    });

    $('<ol/>', {
        'class': 'raw-list',
        html: items.join('')
    }).hide().fadeIn().appendTo('.json');
});

$.getJSON('json/device.json', function (data) {
    var items = [];
    $.each(data[0].status, function (key, val) {
        items.push(val);
    });

    $('<span/>', {
        'class': 'sort-label error',
        html: items.join('')
    }).hide().fadeIn().appendTo('.status-input .sort-label');
});

$.getJSON('json/device.json', function (data) {
    var items = [];
    $.each(data[0].location, function (key, val) {
        items.push(val);
    });

    $('<span/>', {
        'class': 'sort-label',
        html: items.join('')
    }).hide().fadeIn().appendTo('.location-input .sort-label');
});

$.getJSON('json/device.json', function (data) {
    var items = [];
    $.each(data[0].type, function (key, val) {
        items.push(val);
    });

    $('<span/>', {
        'class': 'sort-label',
        html: items.join('')
    }).hide().fadeIn().appendTo('.type-input .sort-label');
});

$.getJSON('json/device.json', function (data) {
    var items = [];
    $.each(data[0].brand, function (key, val) {
        items.push(val);
    });

    $('<span/>', {
        'class': 'sort-label',
        html: items.join('')
    }).hide().fadeIn().appendTo('.brand-input .sort-label');
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T18:19:38+00:00Added an answer on June 18, 2026 at 6:19 pm

    You don’t need multiple $.getJSON calls if you’re retrieving the same data every time.

    For example:

    $.getJSON('json/device.json', function(data) {
    
      // First set of display logic
      var items = [];
      $.each(data[0].attributes, function (key, val) {
         items.push('<li id="' + key + '">' + '  ' + val + '</li>');
      });
    
      $('<ol/>', {
        'class': 'raw-list',
        html: items.join('')
      })
      .hide().fadeIn().appendTo('.json');
    
      // Second set of display logic
      var items = [];
      $.each(data[0].status, function (key, val) {
        items.push(val);
      });
    
      $('<span/>', {
        'class': 'sort-label error',
        html: items.join('')
      })
      .hide().fadeIn().appendTo('.status-input .sort-label');
    
    });
    

    Since you’re using a similar block of code for the display logic of most of the data sets, you could wrap this into a function:

    function displaySortLabel(items, parentClass) {
      $('<span/>', {
        'class': 'sort-label',
        html: items.join('')
      }).hide().fadeIn().appendTo('.' + parentClass +' .sort-label');
    }
    

    And then call it by:

    displaySortLabel(items, "location-input");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

OK so I'm populating a selectbox with a JSON which looks something like this:
I am populating my Google chart using JSON currently, but I need to customize
I am populating a Dojo Combobox dropdown with values from JSON. The code below
I'm populating a datagrid using Linq--standard kind of stuff (code below). For some reason
After populating some divs according to users' selections I want to give them the
im populating the data in the combo box using dataset tables...with some table name..now
I am populating the list view with data I am getting from a JSON
I have a treeview populating from a data set like this Dim PrSet As
i'm populating a datagrid programmatically but before setting the itemsource, i'm also programmatically adding
I'm populating a datatables cell via Ajax and JSON. To get line breaks in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.