Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding multiple(extra) white spaces to span using jQuery

So I have been looking for this question around and still didn't find an answer. I know how to add a single space with the append method in jQuery but don't know how to add multiple spaces.

Example:

<div id="#myDiv"> hey? <span class="space"></span> my name<span class="space"></span> is coding enthusiast<span class="space"></span> .....</di>

I want the output to be like this:

Hey          my name        is coding enthusiast        ......

I have tried Jquery append, it gives me nothing like that.

 //1st try 
 $('space').append("      ");
 //2nd try
 $('space').append("&nbsp &nbsp &nbsp &nbsp");
 //2nd try output: hey&nbsp&nbsp&nbspmy name&nbsp&nbsp&nbspis coding enthusiast&nbsp&nbsp&nbsp....

I feel like I am missing something small. Any ideas?

Edit: Had a typo in my code's question which misled some people when answering:

    //1st try 
     $('.space').append("      ");
     //2nd try
     $('.space').append("&nbsp &nbsp &nbsp &nbsp");
     //2nd try output: hey&nbsp&nbsp&nbspmy name&nbsp&nbsp&nbspis coding enthusiast&nbsp&nbsp&nbsp....
like image 585
Coding Enthusiast Avatar asked May 02 '26 11:05

Coding Enthusiast


2 Answers

fixed, forgot semicolon, Thanks to Jake's comment(which he deleted already), I could fix this issue.

  $('.space').append("&nbsp; &nbsp; &nbsp; &nbsp;");
like image 199
Coding Enthusiast Avatar answered May 04 '26 00:05

Coding Enthusiast


You are not selecting your elements correctly and add semicolon. Try this:

$('.space').append("&nbsp;&nbsp;&nbsp;&nbsp;");
like image 35
Jake Opena Avatar answered May 04 '26 01:05

Jake Opena



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!