2

I know that it is better to manage spacing by means of css rules, but how can I insert spaces between word using jQuery? Consider the following script (see it on JSfiddle) that does not work as expected (spaces are not inserted):

html:

<div id='div1'>
<span>hello</span>
</div>

js:

$("<span>my</span> &nbsp;").insertAfter($("#div1").children("span").eq(0));
$("<span>friends</span> &nbsp;").insertAfter($("#div1").children("span").eq(1));
alert($("#div1").html());   
1
  • What points against using padding? I know you said you know its better to use CSS. But why you need to print spaces? The trick would be &nbsp; to force showing spaces Commented Jun 22, 2017 at 21:04

5 Answers 5

3

It will be easier if you use append:

$("#div1").append(
    '&nbsp;',
    $('<span>').text('my'),
    '&nbsp;',
    $('<span>').text('friends'),
    '&nbsp;'
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='div1'>
<span>hello</span>
</div>

Sign up to request clarification or add additional context in comments.

Comments

1

This line:

$("<span>my</span> &nbsp;")

creates a span element without the ending space like you can see in the following snippet.

You can reduce all chaining to one line:

$("<span>&nbsp;my&nbsp;</span>")
  .insertAfter($("#div1 span:first")).append($("<span>friends&nbsp;</span>"));

console.log('Test: -->' + $("<span>my</span> &nbsp;")[0].outerHTML + '<--')

$("<span>&nbsp;my&nbsp;</span>").insertAfter($("#div1 span:first")).append($("<span>friends&nbsp;</span>"));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id='div1'>
    <span>hello</span>
</div>

Comments

1

Your problem is not with JS but with HTML, you're adding spaces outside of the span element, which has no effect. If you add the space (a regular space will work, entity name is not needed) in the span element, it'll work.

Besides that, you can clear up your code a bit if you use the JQuery append() method. This will also make it so the sentence is in one span element.

<span id="text">hello</span>
<script>
  $("#text").append(" my");
  $("#text").append(" friends");
</script>

Comments

1

One approach is to use .before() to insert a space:

$("<span>my</span>").insertAfter($("#div1").children("span").eq(0)).before("&nbsp;")
$("<span>friends</span>").insertAfter($("#div1").children("span").eq(1)).before("&nbsp;")

JSFiddle Demo: https://jsfiddle.net/c798vsn3/2/

Comments

0

$("<span>&nbsp;my</span>").insertAfter($("#div1").children("span").eq(0));
$("<span>&nbsp;friends</span>").insertAfter($("#div1").children("span").eq(1));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='div1'>
<span>hello</span>
</div>

I updated your fiddle.

You simple have to move the &nbsp;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.