How do I add a non-breaking whitespace in JavaScript without using innerHTML?
I'm generating content dynamically and in some instances, I need to set a as the only content of a <span> element. However, the following adds as text vs adding a empty space: var foo = document.createElement("span") foo = document.createTextNode(" "); which makes sense, so I'm wondering, how would I add correctly without (!) using innerHTML Thanks for help! You can use a unicode literal for a non breaking space : var foo = document.createTextNode("\u00A0"); CodeFanatic If you don't want to use innerHTML , you can use a hexadecimal escape. The most common: \x20 – standard space or \s