Monday, July 4, 2011

iterate array variable in jquery - foreach through jquery

I can across a issue, where i needed to access json response by ajax request.
json response was in array and needed to iterate through each of the records to display it in HTML.

for json response through php I did following
return json_encode($phpArray);

than in the ajax request on html template,
$.ajax({
            type: 'GET',
            url: ##YOURURL##,
            data: '##VARIABLE NAME##='+##VALUE##,
            success: function( jsondata ) {
              //  alert(jsondata);
                var arrObj = jQuery.parseJSON(
jsondata );
                $.each(arrObj, function(index, obj) {
         alert(obj.Id); // here id is any key index in array
         // work with value
       });

            }
        });


In my case, I needed to get the data and insert into an select options.

To create dynamic option for select box, please visit here

1 comment:

  1. I really loved reading your blog. It was very well authored and easy to understand. Unlike additional blogs I have read which really not tht are good. I also found your posts very interesting. In fact after reading, I had to go show it to my friend and he enjoyed it as well! WOW gold

    ReplyDelete