schenker
schenker

Reputation: 953

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

Upvotes: 1

Views: 1811

Answers (2)

KrishCdbry
KrishCdbry

Reputation: 1059

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

Upvotes: 2

Rahul Patel
Rahul Patel

Reputation: 5246

Use &nbsp; to give space. And use html instead of text as below.

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

$("<div>", {
  'class': "main_container",
  'id':'bot1',
  html: 'item_taken&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;item_sold',   //i want to add spaces between
  css: {
    "background-color": "#6699cc"        
  }
}).appendTo('#somediv');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="somediv"></div>

Upvotes: 1

Related Questions