A
A
Alexey2021-02-13 18:37:23
JavaScript
Alexey, 2021-02-13 18:37:23

How to output information from JSON not as a string, but as a list?

Hello everyone, such a question, how to adjust the output of information from JSON not as a string, but as a list. And if it is at all possible, that it displays the values ​​​​already in the format of a button that you can simply click on

function select_company()
{
  var chbx_list = collect_checkboxes();
  var amount = chbx_list.length;
  console.log(chbx_list);
  csrf_token = $('input[name="csrfmiddlewaretoken"]').val();
  $.ajax({
      url : "/blockchain/", // the endpoint убрать первыйс слуеш в случае неудачи
      type : "POST", // http method
      data : {chbx_amount : amount,
              checks_list : chbx_list,
              csrfmiddlewaretoken: csrf_token }, // data sent with the post request

              success : function(json) {
                    // console.log(json);
                document.getElementById('companies').innerHTML = json.companies.join(', ');            

                  },

                  // handle a non-successful response
                  error : function(xhr,errmsg,err) {
                    console.log("error");
                  }
      });
      return false;
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Artyom, 2021-02-13
@Eldenhard

let lisString = "";
for (const company of json.companies) {
    const li = `<li>${company}</li>`;
    lisString+=li;
}
document.getElementById("companies").innerHTML = lisString;

Approximately such code should be inserted into success. Just keep in mind that you need to make the element with id companies ul. Check out this option. It should work if the code that is currently working properly

S
Simkav, 2021-02-13
@Simkav

Get the json, parse it as an array, create the li through document.createElement, then append them all to a list.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question