How to force a line break on a Javascript concatenated string?

后端 未结 4 1078
傲寒
傲寒 2020-12-10 00:25

I\'m sending variables to a text box as a concatenated string so I can include multiple variables in on getElementById call.

I need to specify a line break

相关标签:
4条回答
  • 2020-12-10 00:52
    document.getElementById("address_box").value = 
    (title + "\n" + address + "\n" + address2 + "\n" + address3 + "\n" + address4);
    
    0 讨论(0)
  • 2020-12-10 00:55

    You need to use \n inside quotes.

    document.getElementById("address_box").value = (title + "\n" + address + "\n" + address2 + "\n" + address3 + "\n" + address4)

    \n is called a EOL or line-break, \n is a common EOL marker and is commonly refereed to as LF or line-feed, it is a special ASCII character

    0 讨论(0)
  • You can't have multiple lines in a text box, you need a textarea. Then it works with \n between the values.

    0 讨论(0)
  • 2020-12-10 01:13

    Using Backtick

    Backticks are commonly used for multi-line strings or when you want to interpolate an expression within your string

    let title = 'John';
    let address = 'address';
    let address2 = 'address2222';
    let address3 = 'address33333';
    let address4 = 'address44444';
    document.getElementById("address_box").innerText = `${title} 
    ${address}
    ${address2}
    ${address3} 
    ${address4}`;
    <div id="address_box">
    </div>

    0 讨论(0)
提交回复
热议问题