2

I need to build a list using the parameters "name" taken from my file "sendjson.php" (in json, of course). How can i create a list using jQuery like this one?

<div id="firstresultname">Firstresultname</div>
<div id="secondresultname">Secondresultname</div>

I don't know how much result will be so is possible to detect and write the right number on div? Thanks!

3
  • 6
    please define "list". please show sendjson.php. Commented Jul 20, 2011 at 21:33
  • can you tell where you are having the problem? specify the json response you are getting and how you want to get it displayed... Commented Jul 20, 2011 at 21:34
  • For list i mean a series of elements ... this is an example of the result fom sendjson.php: {"items":[{"name":"iPod Shuffle"},{"name":"Nintendo Wi"}]} - i need to show one result for each div. using the example i posted in json, it should become <div>iPod shuffle</div> <div>Nintendo Wii</div> Commented Jul 20, 2011 at 21:38

1 Answer 1

9
$.each(json, function(key, value){
    $("body").append('<div id="'+value+'">'+value+'</div>');
});

or just

$.each(json, function(key, value){
    $("body").append('<div>'+value+'</div>');
});

to retreive json & proccess, do following

$.ajax({
    'url' : 'yoururl.php',
    dataType: 'json',
    success: function(json){
        $.each(json, function(key, value){
            $("body").append('<div>'+value+'</div>');
        });
    }
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.