Top community members
All Wiki Articles Create Wiki Article

Many years me and my friends we were volunteers on my university doing programming classes for young people.

I got main conclusion: Young people are awesome, especially students, because they always bring new and fresh ideas :)
* I loved to do it but my time is limited

  I decided to create something that will help millions of students

Now I am here

co-founder

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

0 contributions
3 points

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

Quick solution: use this following \u00A0 character code.

// ONLINE-RUNNER:browser;

var text = "a \u00A0\u00A0\u00A0 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\u00A0 or \u00a0  or   

Binding non-breaking space to HTML from JavaScript example.

There are few ways how to do it:

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

Check below example:

// ONLINE-RUNNER:browser;

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

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

    element.innerText = 'a \u00A0\u00A0\u00A0 b';

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

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

 

0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more