Reputation: 17
I want to do some loop with the javascript and getting issue. What i need is to get JSON result from the query:
devices[1].data.deviceTypeString.value
devices[2].data.deviceTypeString.value
etc Here is the code.
<script>
for (var x=0; x<5; x++)
{
$.getJSON('0', function(data) {
var output = 'data.devices[' + x + '].data.deviceTypeString.value;'
document.write(output + "<br />");
});
}
</script>
THe problem is that I am not getting result of JSON. I am getting result exactly like this:
data.devices[0].data.deviceTypeString.value;
data.devices[1].data.deviceTypeString.value;
data.devices[2].data.deviceTypeString.value;
data.devices[3].data.deviceTypeString.value;
data.devices[4].data.deviceTypeString.value;
Please help.
Upvotes: 0
Views: 53
Reputation: 16706
Replace
var output = 'data.devices[' + x + '].data.deviceTypeString.value;'
With
var output=data.devices[x].data.deviceTypeString.value;
and pls don't use document.write
..
also read about javascript
before using jquery
.
btw you are using ajax and it should be called sequentially if multiple json files.. but i guess you need to call it once... so:
$.getJSON('0', function(data) {
for (var x=0; x<5; x++){
var output=data.devices[x].data.deviceTypeString.value,
div=document.createElement('div');
div.textContent=output;
document.body.appendChild(div);
}
});
or
$.getJSON('0',function(data){
for(var x=0,out='';x<5;x++){
out+=data.devices[x].data.deviceTypeString.value+'<br>',
}
document.body.innerHTML=out;
});
Upvotes: 1