user1427811
user1427811

Reputation:

Adding space in a string jquery

I wanto to insert a space at the start of my string Example:

MySite
   MySite (I want this)

I have tried in this mode but nothing

txt="    "+v.text();

Can someone help me? Thanks a lot

Upvotes: 0

Views: 25465

Answers (4)

kiran kumar
kiran kumar

Reputation: 1422

var str2 = "          Here goes your string with spaces";

Upvotes: 1

Durgaprasad Budhwani
Durgaprasad Budhwani

Reputation: 977

There are 2 solutions below :-

1. txt="<pre>    </pre>"+v.text();


2. txt="<span style='margin-left:15px'></span>"+v.text();

Upvotes: 0

Fabrizio Calderan
Fabrizio Calderan

Reputation: 123367

A solution is to fill the spaces with &nbsp; entities, but if you have to achieve this effect just for styling/visualization purpose, then wrap that line in a proper tag and use some CSS instead (e.g. text-indent property)

if you have to indent text inside a select (as I guess reading your previous answer) you may want to wrap your options inside an optgroup element (and give it some padding)

Upvotes: 5

sushil
sushil

Reputation: 2671

Try :

 txt="&nbsp;&nbsp;&nbsp;&nbsp;"+v.text();

The other good solution would be to use div with some style set on it.

 <div style='margin-left:15px' id='main_text'></div>

You could use jQuery to place txt in the div.

     txt= v.text();
     $('#main_text').html(txt);

Upvotes: 7

Related Questions