netpumber Posted April 29, 2014 Share Posted April 29, 2014 Hi! Server sends such a json object: {"status":1,"recId":"PL-17534","collectionDate":"08-04-2014","collectorsName":"asdf","donorsName":"","sciName":"asdf","family":"asdf","comName":"asdf","variety":"","area":"asdf","photoFiles":["1.jpg","internet.jpg"]} So i want to change the html content of an element , by customizing the contents of json object in the photoFiles array. I tried something like this but doesn't seem to work $('#photosTab').html(function(){ $.each(json.photoFiles, function(i,item){ return '<img src="'+json.photoFiles[0]+'"><br>'; }) }); Any idea ? Quote Link to comment https://forums.phpfreaks.com/topic/288113-jquery-each-within-a-json-array/ Share on other sites More sharing options...
goodacre.liam Posted May 26, 2014 Share Posted May 26, 2014 You probably want something like: // Renders a single item as html var renderItem = function (item) { return '<img src="' + encodeURI(item) + '">' } // Render and display all the photo file items $('#photosTab').html($.map(json.photoFiles, renderItem)) Before you get unnecessarily far down the rabbit hole with manual dom interaction, take a look into Facebook's React library. It allows you to program much more intuitively, and simply by saying that your 'view' is a function of the state of your application at any point in time. Hope this helps. Quote Link to comment https://forums.phpfreaks.com/topic/288113-jquery-each-within-a-json-array/#findComment-1480860 Share on other sites More sharing options...
Drongo_III Posted May 26, 2014 Share Posted May 26, 2014 The example code below returns the values of the photoFiles array: var json = {"status":1,"recId":"PL-17534","collectionDate":"08-04-2014","collectorsName":"asdf","donorsName":"","sciName":"asdf","family":"asdf","comName":"asdf","variety":"","area":"asdf","photoFiles":["1.jpg","internet.jpg"]}; $.each(json.photoFiles, function(key, val){ //return '<img src="'+val+'"><br>'; console.log( '<img src="'+val+'"><br>'); }); I'm not sure I fully understand what you are trying to achieve by returning in an anonymous function but that code works to give you access to each of the values. Quote Link to comment https://forums.phpfreaks.com/topic/288113-jquery-each-within-a-json-array/#findComment-1480963 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.