Build DOM elements from JSON / JQuery

erzoolander
erzoolander used Ask the Experts™
on
I've got a JSON string that takes this form...

{"people":[{"name":"person1","description":"This is a brief description of my service.","image":"mandg1.jpg","type":"first","insured":"yes","license":"N\/A","picture":"mandg1.jpg","yearsinbusiness":"8","distance":15},{"name":"person2","description":"This is a brief description of my service.","image":"mandg2.jpg","type":"second","insured":"yes","license":"N\/A","picture":"mandg2.jpg","yearsinbusiness":"8","distance":19}]}

What I'd like to do is, via jQuery, build a group of divs based upon the "people" key...so in the instance above...you'd end up creating 2 elements...and append them to the div id="container".  If there were 50 elements, you'd end up with 50 divs, etc etc etc.

<div id="container">
<div id="person1">
<p>Name: person1</p>
<p>Description: This is a brief description of my service</p>
</div>
<div id="person2">
<p>Name: person2</p>
<p>Description: This is a brief description of my service</p>
</div>
</div>

What would the jQuery syntax for that be?  Thanks in advance!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Expert of the Year 2008
Top Expert 2008
Commented:
Try:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Untitled</title>
<style type="text/css">
<!--
#container {border:1px solid red;}
.person{border:1px solid green;margin:2px;}
-->
</style>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript">
var data = {
	"people": [{
		"name": "person1",
		"description": "This is a brief description of my service.",
		"image": "mandg1.jpg",
		"type": "first",
		"insured": "yes",
		"license": "N\/A",
		"picture": "mandg1.jpg",
		"yearsinbusiness": "8",
		"distance": 15
	}, {
		"name": "person2",
		"description": "This is a brief description of my service.",
		"image": "mandg2.jpg",
		"type": "second",
		"insured": "yes",
		"license": "N\/A",
		"picture": "mandg2.jpg",
		"yearsinbusiness": "8",
		"distance": 19
	}]
}

$(function(){
	$('<div id="container"/>').appendTo('body');

	$.each(data.people,function(index,item){
		var div = $('<div id="'+item.name+'" class="person"/>').appendTo('#container');
		$('<p>Name: '+item.name+'</p>').appendTo(div);
		$('<p>'+item.description+'</p>').appendTo(div);
	});

});
</script>
</head>
<body>
	
</body>
</html>

Open in new window

Top Expert 2011
Commented:
I broke it into steps to make it easy to follow/modify:

var theStuff = {"people":[{"name":"person1","description":"This is a brief description of my service.","image":"mandg1.jpg","type":"first","insured":"yes","license":"N\/A","picture":"mandg1.jpg","yearsinbusiness":"8","distance":15},{"name":"person2","description":"This is a brief description of my service.","image":"mandg2.jpg","type":"second","insured":"yes","license":"N\/A","picture":"mandg2.jpg","yearsinbusiness":"8","distance":19}]};
var thePeople = theStuff['people'];

var htmlPeople = $.map(thePeople,function(i){
  var thisHTML = '<div id="' + i.name + '"><p>Name: ' + i.name + '</p><p>Description: ' + i.description + '</p></div>';  
  return (thisHTML);
}).join('');

var theContainer = '<div id="container">' + htmlPeople + '</div>';

Open in new window


This does not have any error handling in it (if the JSON contains an apostrophe, the whole thing breaks).  

Also, you probably want to use the index of the object to ID your divs rather than the person's name like the example shows:

Removing the intermediate steps and using the index instead of the name as the div id looks like this:
var theStuff = {"people":[{"name":"person1","description":"This is a brief description of my service.","image":"mandg1.jpg","type":"first","insured":"yes","license":"N\/A","picture":"mandg1.jpg","yearsinbusiness":"8","distance":15},{"name":"person2","description":"This is a brief description of my service.","image":"mandg2.jpg","type":"second","insured":"yes","license":"N\/A","picture":"mandg2.jpg","yearsinbusiness":"8","distance":19}]};

var htmlPeople = '<div id="container">' + $.map(theStuff['people'],function(i,idx){
  var thisHTML = '<div id="person' + idx + '"><p>Name: ' + i.name + '</p><p>Description: ' + i.description + '</p></div>';  
  return (thisHTML);
}).join('') + '</div>';

Open in new window

Author

Commented:
Gracias!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial