简体   繁体   中英

How to add line break in javascript

I am trying to add the line break from javascript for this snippet

function getCity(city_name, product_cat){
  var writecity = document.createTextNode(city_name+','+product_cat);
  document.getElementById("order_list").appendChild(writecity,'<br>');
}

I am calling this function in for loop, but the values printed are all coming in the same line. but i want to add the break line after each value printed. how can i do that?

.appendChild() takes only one argument, so the second string( <br> ) passed to it is ignored.

You need to call appendChild() again to add the br element

function getCity(city_name, product_cat) {
    var writecity = document.createTextNode(city_name + ',' + product_cat);
    var el = document.getElementById("order_list");
    el.appendChild(writecity);
    el.appendChild(document.createElement('br'));
}

Try document.createElement :

document.getElementById("order_list").appendChild(writecity);
document.getElementById("order_list").appendChild(document.createElement('br'));

appendChild only takes one argument, I think. So you'll have to append another child:

function getCity(city_name, product_cat){
   var writecity = document.createTextNode(city_name+','+product_cat);
   var break = document.createElement('br');
   document.getElementById("order_list").appendChild(writecity);
   document.getElementById("order_list").appendChild(break);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM