jquery – Add JavaScript String with line breaks and tabs to HTML element

jquery – Add JavaScript String with line breaks and tabs to HTML element

You should tell browser to respect those whitepaces. Easy way to do it is using white-space: pre rule:

var data = Heading (info:info) n info: n t and so on ; 

$(#element).html(data);
#element {
  white-space: pre;
}
<script src=https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js></script>
<p id=element>Waiting for Message</p>

Make use of the pre tag in HTML and update your text there

$(document).ready(function(){

var data = {
    id: 1,
    nome: erwrw,
    cognome: sdsfdfs,
    CF: qwert,
    eta: 27,
    sesso: uomo,
    indirizzo: qwerrt,
    luogo: wewrw,
    provincia: ewrewrw,
    citta: erwrwr,
    comune: ewrewrw
  }

var obj = JSON.stringify(data, null, 4);
$(#element).text(obj);
})
<script src=https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js></script>
<pre id=element>Waiting for Message</pre>
<button id=btn>send</button>

jquery – Add JavaScript String with line breaks and tabs to HTML element

$(#element).html(data.replace(/(n)/g, <br>));

Leave a Reply

Your email address will not be published. Required fields are marked *