0

My json is

[{"dir":"down","sym":"oo","b":"1.35553","a":"1.35556"},
{"dir":"down","sym":"pp","b":"1.64442","a":"1.64451"},
{"dir":"down","sym":"qq","b":"104.603","a":"104.609"},
{"dir":"up","sym":"rr","b":"0.88186","a":"0.88191"},
{"dir":"up","sym":"ss","b":"1.09667","a":"1.09672"}]

using this jquery

<script type="text/javascript">
$(document).ready(function() {
setInterval(function() {
$.ajax({
    cache: false,
        url: 'quotes1.php',
        dataType: 'json',
    success: function(json){
        for(var i=0;i<json.length;i++){
            $('#divToRefresh').html(json[i].dir + '/' + json[i].sym+ '/' + json[i].b+ '/' + json[i].a);
        }
    }
});
}, 1000);  
});
</script>

But it displays 1st data only

up/oo/1.35553/1.35556

How can I display all the data?

6 Answers 6

3

Use jQuery $.each() and append() method for that

success: function(json){
    $.each(json,function(i,val){
        $('#divToRefresh').append(val.dir + '/' + val.sym+ '/' + val.b+ '/' + val.a);
    });
}

html() will replace existing data hence it only display the last value , but append() will help to concatenate data.

$.each is better than the for loop.

Sign up to request clarification or add additional context in comments.

Comments

1

Use .append() instead of .html(). You can also use $.each()

success: function(json){
    $.each(json,function(i,value){
        $('#divToRefresh').append(value.dir + '/' + value.sym+ '/' + value.b+ '/' + value.a);
    });
}

Comments

0
 $('#divToRefresh').append(json[i].dir + '/' + json[i].sym+ '/' + json[i].b+ '/' + json[i].a);

.html() will replace content .append() will concate the content

Comments

0

Instead of the below code:

for(var i=0;i<json.length;i++){
            $('#divToRefresh').html(json[i].dir + '/' + json[i].sym+ '/' + json[i].b+ '/' + json[i].a);
        }

Use the below code:

$.each(JSON.parse(json), function(key,val){
  $('#divToRefresh').append(val.dir + '/' + val.sym+ '/' + val.b+ '/' + val.a);
});

1 Comment

This will only ever display the last value? html will replace the content for every item in the loop
0

That's strange: I would have thought that it would rather display the last...

Try this:

    for(var i=0;i<json.length;i++){
        $('#divToRefresh').append("<p>" + json[i].dir + '/' + json[i].sym+ '/' + json[i].b+ '/' + json[i].a + "</p>");
    }

Comments

0
<script type="text/javascript">
$(document).ready(function() {

setInterval(function() {
$.ajax({
    cache: false,
        url: 'quotes1.php',
        dataType: 'json',
        success: function(json){
        var dd="";
        for(var i=0;i<json.length;i++){
            dd += json[i].dir + '-' + json[i].sym+ '-' + json[i].bid+ '-' + json[i].ask+'</br>';
        }
        $('#divToRefresh').html(dd);
    }
});
}, 1000);  
});
</script>

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.