Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create A Blank Html Space On The Fly Javascript

Tags:

javascript

How do I create a blank space on the fly using javascript? The following is not working for me..

var blank = document.createElement(" "); div.appendChild(blank); 
like image 442
Nick LaMarca Avatar asked Jun 08 '12 15:06

Nick LaMarca


People also ask

How do I make a blank space in HTML?

HTML Non-Breaking Space ( ) The simplest way to add a space in HTML (besides hitting the spacebar) is with the non-breaking space entity, written as   or  .

How do you make a space in JavaScript?

A common character entity used in HTML is the non-breaking space ( ). Remember that browsers will always truncate spaces in HTML pages. If you write 10 spaces in your text, the browser will remove 9 of them. To add real spaces to your text, you can use the   character entity.

How do you put a space in a span tag?

We can use the padding-left property in a <span> element to add spaces. For example, inside the HTML body, type the text of your desire. Where you want to add the blank space, create a <span> element and apply the style in it. Set the padding-left property to 30px .

What is white space in HTML?

Whitespace refers to characters which are used to provide horizontal or vertical space between other characters. Whitespace is often used to separate tokens in HTML, CSS, JavaScript, and other computer languages.


1 Answers

Try using this:

myElement.appendChild( document.createTextNode( '\u00A0' ) ); 

If you want/need more spaces, use several \u00A0 e.g.:

myElement.appendChild( document.createTextNode( '\u00A0\u00A0' ) ); 

This one:

document.createEntityReference('nbsp'); 

doesn't work all browsers, so avoid it.

like image 185
Moin Zaman Avatar answered Oct 11 '22 21:10

Moin Zaman