schenker schenker - 2 months ago 12
Javascript Question

How do i add space characters to text in jquery appendto?

i want to add additional space characters in my code in Jquery appendto. How can i add it programmatically? I have searched a number of discussions, but none are quite similar to my problem. My code shown below:

$("<div>", {
'class': "main_container",
'id':'bot1',
text: 'item_taken | item_sold', //i want to add spaces between
css: {
"background-color": "#6699cc"
}
}).appendTo('#somediv');


Thank you

Answer

Use HTML instead of text and you can use [&nbsp] or [&emsp] (Equal to tab)

$("<div>", {
 'class': "main_container",
 'id':'bot1',
 html: 'item_taken &emsp; | &emsp; item_sold',  
 css: {
   "background-color": "#6699cc"        
 }
}).appendTo('#somediv');

Working demo : https://jsbin.com/saxogo/edit?html,js,output