Ajax update from JSON data- index loop

Hi,

In the following example getjsondata.php returns
{"id1":"Banana","id2":"Apple","id3":"Pear"}

and I update each element manually using the json data , thus

$.getJSON("getjsondata.php", function(data) {                        
      $("#id1").html(data.id1);
      $("#id2").html(data.id2);
      $("#id3").html(data.id3);
});      

How can I iterate through the json array to update each element returned in the json string
In otherwords, looking for something like

keys = getkeys(data)
foreach key in keys {
 $(key).html = data.[key]
}

As a bonus, I would like to create a <div id='xxx'>value</div> when an array index returned in the json data does not already exist in the DOM

Thanks
BT
Complete code:

<html>
<head>
	<script src='scripts/json2.js' type='text/javascript'></script>
	<script type="text/javascript" src="scripts/jquery-1.4.2.min.js"></script>


		<script type="text/javascript">
		$(function(){

			
			$('#update').click ( function () {
				$.getJSON("getjsondata.php", function(data) {				
				$("#id1").html(data.id1);
				$("#id2").html(data.id2);
				$("#id3").html(data.id3);
				});			
			});
		});
			
				
		</script>
		
</head>
<body>
	
	<p><a id='update' href='#'>Update</a></p>
	<div id='id1'></div>
	<div id='id2'></div>
	<div id='id3'></div>
</body>
</html>

Open in new window

brothertomAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hieloCommented:

<html>
<head>
        <script src='scripts/json2.js' type='text/javascript'></script>
        <script type="text/javascript" src="scripts/jquery-1.4.2.min.js"></script>


                <script type="text/javascript">
                $(function(){

                        
                        $('#update').click ( function () {
                                $.getJSON("getjsondata.php", function(data) {  
						  /*                         
                                $("#id1").html(data.id1);
                                $("#id2").html(data.id2);
                                $("#id3").html(data.id3);
						  */
						  for( var k in data)
						  {
						  	$("#"+k).html( data[k] );
						  }
                                });                     
                        });
                });
                        
                                
                </script>
                
</head>
<body>
        
        <p><a id='update' href='#'>Update</a></p>
        <div id='id1'></div>
        <div id='id2'></div>
        <div id='id3'></div>
</body>
</html>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
brothertomAuthor Commented:
Cool - thanks for that - any thoughts on testing if an element exists and adding it to the DOM if not?

Thanks
BT
0
hieloCommented:
jquery handles that for you internally (silently - meaning no errors will be triggered. Simply nothing will be inserted since it cannot find the element).
0
brothertomAuthor Commented:
Useful to know about the no error thing - I'll raise a more suitable question about adding elements.

Cheers
BT
0
brothertomAuthor Commented:
Many thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.