Vladimir Štus Vladimir Štus - 1 year ago 74
Javascript Question

Json loop and appending results to html

I have a problem with JavaScript loop, because I use keyup function i cant use

function because it add only last result, and with append it doubles results.

How to solve this problem?

for (var i = 0; i < msg.length; i++)
$('#result').html(' ' + msg[i].info + '</br> ');

Answer Source

With the current code you show above, you remove the content of #result for every array element of msg. You need to append the result if you want every array element shown. If you get double as you say, then you have a problem with the returning result from your script.

The correct approach is:

$('#result').html(''); //If you want to empty the container before posting results to it
for(var i=0; i<msg.length; i++) {
    $('#result').append(' ' + msg[i].info + '</br> ');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download