Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Noob place array into innerHTML with elements separated by newline

Tags:

javascript

i am trying to place array into innerHTML but add a newline after each element,

var ingr_list = [];

//then i push some items

if (checkBox.checked == true){
    text.style.display = "none"; 
    ingr_list.push(nameofingredient);
    document.getElementById('ingredienttype').innerHTML = ingr_list;

As for now it just show array as entire line which ruins my responsiveness (the longer the array gets the wider screen grow which i do not know how to fix - \n or would do the trick)

like image 786
jdow Avatar asked Mar 06 '26 11:03

jdow


1 Answers

Join the array elements with <br> tags like this:

var output = ingr_list.join('<br>');
document.getElementById('ingredienttype').innerHTML = output;
like image 122
Randy Casburn Avatar answered Mar 08 '26 02:03

Randy Casburn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!