Languages
[Edit]
EN

JavaScript - no-break / non-breaking space in string

8 points
Created by:
maryam
1031

In this very short article, we are going to look at how to insert a no-break (non-breaking) space character to string in JavaScript.

Quick solution:

Use the following \xA0 character code.

// ONLINE-RUNNER:browser;

var text = 'a \xA0\xA0\xA0 b';  // U+00A0

console.log(text);  // a     b

 

Non-breaking space in unicode:

Unicode code
(by hex number)

JavaScript
(by hex number)

HTML
(by dec or hex number)

HTML
(by name)

U+00A0

\xA0 or \xa0
\u00A0 or \u00a0

 ,   or   

Binding non-breaking space to HTML from JavaScript example.

There are a few ways how to do it:

  • by pasting code directly into JavaScript string - some codes could be invisible without hex editor,
  • with innerText or textContent property and unicode character code,
  • with innerHTML property and html unicode code (e.g.    or  ).

Check the below example:

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<body>
  <div id="element"></div>
  <script>

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

    element.innerText = 'a \xA0\xA0\xA0 b';           // or  \u00A0

    // element.textContent = 'a \xA0\xA0\xA0 b';      // or  \u00A0
    // element.innerHTML = 'a &nbsp;&nbsp;&nbsp; b';  // or  &#xA0;  or  &#160;

  </script>
</body>
</html>

 

Alternative titles

  1. Example how to bind non-breaking space to HTML from JavaScript
  2. JavaScript - no-break/non-breaking white space in string
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.

JavaScript - String (popular problems)

JavaScript - no-break / non-breaking space
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