Home > Enterprise >  How to apply & nbsp on html while using innerText method in javascript?
How to apply & nbsp on html while using innerText method in javascript?

Time:11-10

I am making a terminal app in javascript. But the problem is that I can't use innerHTML method. I tried using innerText method, but it doesnt apply multi space and & nbsp; is also ignore. For example:

example.innerText = "a  b"; //output is not 'a  b'

I would be glab if someone could help me.

CodePudding user response:

  works for HTML, but not for text. .innerText deals with visible text while .textContent recognizes visible and hidden text, see this answer for details.

document.querySelector(".test").textContent = "A\u00A0\u00A0B";
<p>AB</p>
<p ></p>

CodePudding user response:

example.textContent = 'This\u00a0 thing'

Or

example.textContent = 'This\u00a0\u00a0thing'

CodePudding user response:

Why use non-breaking spaces when you can use CSS and normal spaces? If you're writing a terminal app, pretty sure you need white-space: pre already anyway since you're probably using a monospace font and use spaces to align ouput, so just keep using that:

.preeformatted { white-space: pre; }
code { background: #DDD; font-variant: small-caps; font-weight: bold; }
<p >let's use normal spaces instead of <code>&amp;nbsp;</code>:   a    b</p>

  • Related