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);
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  .
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.
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 .
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With