Solved

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

Posted on 2016-10-18
4
38 Views
Last Modified: 2016-10-19
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
0
Comment
Question by:karthik80c
  • 3
4 Comments
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
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

1
 
LVL 51

Accepted Solution

by:
Julian Hansen earned 500 total points
Comment Utility
A note on your data. You have an array of one element  - an object with the properties you are interested in.

Therefore everything has to be relative to index 0 of the array. In your case I am assuming that the name of the variable holding the structure is

propertyData

Open in new window


Therefore you want to access the properties of
propertyData[0]

Open in new window


Tried with for loop i got only one picture 0
This is the reason - you were iterating over the array - which only has one element.

You can also use
for(var i in propertyData[0])

Open in new window

To iterate over the methods in the object rather than using propertyData.length
1
 

Author Closing Comment

by:karthik80c
Comment Utility
Thanks Julian . Its Working
0
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
You are welcome.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now