Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use jQuery to convert JSON array to HTML bulleted list

Tags:

json

jquery

How can you convert an array of strings represented in JSON format and convert this to an HTML bulleted list using jQuery?

like image 931
Marcus Leon Avatar asked Nov 15 '10 22:11

Marcus Leon


1 Answers

var ul = $('<ul>').appendTo('body');
var json = { items: ['item 1', 'item 2', 'item 3'] };
$(json.items).each(function(index, item) {
    ul.append(
        $(document.createElement('li')).text(item)
    );
});

As far as fetching the JSON from your server using AJAX is concerned you could use the $.getJSON() function.

Live demo.

like image 140
Darin Dimitrov Avatar answered Sep 18 '22 19:09

Darin Dimitrov