Print array list values ​​with loop for div dynamically

I have an array called desc, which contains some text for each of its values ​​and changes the 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, and does not print each of 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 print a complete list with each array value under the last using a loop?

+6
source share
6 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); } 
+7
source

Or simply:

 $('#container').text(desc.join('\r\n')); 
+1
source
 function incidentList(){ for(var i=0; i<desc.length; ++i){ $("#container").text( $("#container").text() + desc[i] + '<br/>'); } } 
0
source

You need to use jQuery.append, not .text

0
source
  function incidentList(){ for(var i=0; i<desc.length; ++i){ $("#container").append(desc[i] +"<br/>"); } } 

it should work

0
source

try it

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

the .text() method will replace the contents of the div, but .append() will be added so that you display all the displayed data.

0
source

Source: https://habr.com/ru/post/959045/


All Articles