Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print array list values with loop to div dynamically

I have an array called desc that contains some text for each of it's values and change's length and values depending on what the user clicks on.

array:

desc[0] =  "manhole cover on foothpath on barrog gaa grounds kilbarrack road loose."
desc[1] =  "Footpath at driveway to 17 Maywood Lawn in bad state of disrepair."

I would like to display these array values in a div called #container. At the moment it just prints the last value of the array in #container rather that printing each of the the values in the list.

JavaScript:

 function incidentList(){
            for(var i=0; i<desc.length; ++i){
             $("#container").text(desc[i]);
        }
      }

Html:

<div id="container" style="width: 50%; height:97%; float:right;"></div> 

How should I go about printing the full list with each array value underneath the last using a loop?

like image 512
wtmcm Avatar asked Nov 29 '13 12:11

wtmcm


2 Answers

function incidentList(){
  var full_list = ""
  for(var i=0; i<desc.length; ++i){
      full_list = full_list + desc[i] + '<br>'
  }
  $("#container").text(full_list);     
}
like image 61
AlvaroAV Avatar answered Oct 30 '22 23:10

AlvaroAV


Or simply:

$('#container').text(desc.join('\r\n'));
like image 1
kayen Avatar answered Oct 31 '22 00:10

kayen