Languages

How to use   in HTML set via innerText property in JavaScript?

8 points
Asked by:
Lily
548

I am trying to set nbsp from JavaScript source code but it doesn't work.

Simplified code:

// ONLINE-RUNNER:browser;

<div id="element"></div>
<script>

    var element = document.querySelector('#element');

    element.innerText = 'Word 1&nbsp;&nbsp;&nbsp;Word 2&nbsp;&nbsp;&nbsp;Word 3';

</script>

Any idea what is wrong?

1 answer
6 points
Answered by:
Lily
548

Working with innerText property use \u00A0.

// ONLINE-RUNNER:browser;

<div id="element"></div>
<script>

    var element = document.querySelector('#element');

    element.innerText = 'Word 1\u00A0\u00A0\u00A0Word 2\u00A0\u00A0\u00A0Word 3';

</script>

 

References

  1. JavaScript - no-break / non-breaking space in string
0 comments Add comment
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join