score:10

Accepted answer
d3_selectionPrototype.text = function(value) {
    return arguments.length < 1 ? this.node().textContent : this.each(typeof value === "function" ? function() {
      var v = value.apply(this, arguments);
      this.textContent = v == null ? "" : v;
    } : value == null ? function() {
      this.textContent = "";
    } : function() {
      this.textContent = value;
    });
  };

The important line here is that calling .text() on a selection will set the textContent property on each element. So the correct thing to do in your case is use

selection.text(' ');

So if you want to use spaces then you simply have to use spaces in the text, not html encoded spaces. If you want to use html encoded chars then you have to treat the content as innerHTML.

selection.html('&nbsp;');

score:40

Use a JavaScript Unicode escape rather than an HTML entity. &nbsp; in HTML denotes the Unicode character U+00A0 so

selection.text('\u00A0')

should do what you want.


Related Query

More Query from same tag