Link to home
Create AccountLog in
Avatar of karthik80c
karthik80cFlag for United States of America

asked on

How do i create a element based on server response in Jquery

Hi Experts ,

I want to create a element based on json response from server . Here is my json look like

[
  {
    "agentname": "Test Hari",
    "contact": "",
    "office": "",
    "mytours": "",
    "profilepic": "http://www.freyvogelfuneralhome.com/sitebuildercontent/sitebuilderpictures/heartc
.jpg",
    "Price": "",
    "Map": "",
    "Address": "test hari",
    "Photo1": "upload/image1.JPG",
    "Photo2": "upload/image1.JPG",
    "Photo3": "upload/image1.JPG",
    "Photo4": "upload/image1.JPG",
    "Photo5": "upload/image1.JPG",
    "Photo6": "upload/image1.JPG",
    "Photo7": "upload/image1.JPG"
  }
]

Open in new window


and i need each of the Photo1 ,2 ,3 to create a element like  this
<li><a href="#" data-image="upload/image1.JPG" class="active">Guest Bedroom 1</a></li>

Open in new window


tried with for loop i got only one picture 0

 for (var i = 0; i < propertyData.length; i++) {
 var content = ' <li><a href="#" '+propertyData[i]['Photo'+i]+' class="active">Picture '+i+'</a></li>';
$('#k-photos-labels-wrapper').append(content);

Open in new window


Any Suggestions on this
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Here is a sample
<script>
var data = [
  {
    "agentname": "Test Hari",
    "contact": "",
    "office": "",
    "mytours": "",
    "profilepic": "http://www.freyvogelfuneralhome.com/sitebuildercontent/sitebuilderpictures/heartc.jpg",
    "Price": "",
    "Map": "",
    "Address": "test hari",
    "Photo1": "upload/image1.JPG",
    "Photo2": "upload/image1.JPG",
    "Photo3": "upload/image1.JPG",
    "Photo4": "upload/image1.JPG",
    "Photo5": "upload/image1.JPG",
    "Photo6": "upload/image1.JPG",
    "Photo7": "upload/image1.JPG"
  }
];
$(function() {
	for(var i in data[0]) {
	  var m = i.match(/Photo(\d+)/);
	  if (m) {
		html = "Guest Bedroom " + m[1];
		var a = $('<a/>').addClass('active').html(html).data('image', data[0][i]);
		$('<li/>').append(a).appendTo('#k-photos-labels-wrapper');
	  }
	}
});
</script>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of karthik80c

ASKER

Thanks Julian . Its Working
You are welcome.