Search code examples
jquerytrimcreate-table

jquery prevent auto trim while dynamically add table


I use jquery for adding table dynamically and I use this pattern:

var tbl = $("<table></table>").addClass("messageTable");
var tblRow = $("<tr></tr>").addClass("messageRow").appendTo(tbl);
var tblCol1 = $("<td></td>").text(username+":").addClass("sender").appendTo(tblRow);
var tblCol2 = $("<td></td>").text(message).css({"color":color}).appendTo(tblRow);

When I have 'message' value starts with all spaces, then it looks like this mechanism automatically trims the text. I am sure because I am alerting message before create new table

Ex: message=' xxx' then table column= 'xxx'

Can you help me how can I prevent auto trimming?

Thanks


Solution

  • It is not the auto trimming, It is just that any space inside td will be ignored. Try the below CSS and it should work,

    td { white-space:pre-wrap; }
    

    DEMO here