Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8844511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:30:21+00:00 2026-06-14T11:30:21+00:00

It seems that when using Knockout’s text binding, multiple spaces become collapsed into one.

  • 0

It seems that when using Knockout’s text binding, multiple spaces become collapsed into one. For example:

<textarea data-bind="value: Notes"></textarea>
<p data-bind="text: Notes"></p>

function VM() {
this.Notes = ko.observable();
}

var vm = new VM();

ko.applyBindings(vm);

Here is a fiddle to demonstrate this: http://jsfiddle.net/9rtL5/

I am finding that in jsfiddle, the spaces are compacted in Firefox, Chrome and IE9. Strangely though within my app IE9 does not compact them, but the others do.

My understanding is that Knockout uses an HTML text node to render the value. I found this related question on preserving spaces when creating a text node:

http://www.webdeveloper.com/forum/showthread.php?193107-RESOLVED-Insert-amp-nbsp-when-using-createTextNode%28%29

Should Knockout handle transforming spaces appropriately? I don’t really want to use a custom binding handler for this.

I actually came across this in the context of the display text within a select, and only discovered that it also relates to a simple text binding while debugging that issue. I presume the select issue is the same.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T11:30:22+00:00Added an answer on June 14, 2026 at 11:30 am

    What you are observing is normal behavior. When rendered in certain elements, the whitespace is trimmed. Knockout shouldn’t be doing any automatic replacements, if I wanted to send a string to a server with leading/trailing spaces using knockout, it better make it there with those spaces.

    You should create a binding handler to replace the spaces with no-breaking spaces so it can be rendered that way.

    ko.bindingHandlers.spacedtext = {
        replaceSpace: function (str) {
            return new Array(str.length + 1).join('\xA0');
        },
        init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
            var spacedValue = ko.computed(function () {
                var value = ko.utils.unwrapObservable(valueAccessor()),
                    text = value && value.toString();
                return text && text.replace(/^\s+|\s+$/gm, ko.bindingHandlers.spacedtext.replaceSpace);
            });
            ko.applyBindingsToNode(element, { text: spacedValue });
        }
    };
    

    Demo

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using knockout.js I've run into this problem a couple of times now. It seems
It seems that using addErrorMessage() overrides all other validation errors. For example, I created
It seems that using Critical Sections quite a bit in Vista/Windows Server 2008 leads
It seems to me that using unanchored namespaces is just asking for trouble later
It seems that licensing terms would prevent us from using Google Maps API in
It seems that SQL Server does not accept numbers formatted using any particular locale.
It seems to be the prevailing opinion that using protected data members is a
I am using an iframe in my project and it seems that whenever it
I developed a site using twitter bootstrap and it seems that the responsive layout
I'm using the stackmob API in an android project and it seems that the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.