4

I've been trying to set content of a text input dynamically using JS, the problem I encountered is I can not have the browser render the special symbols rather than chars so for example

document.getElementById("textField").value = "nbsp";

Instead of displaying a space it displays &nbsp, anybody got any idea?

Thanks a lot

user1935724
  • 554
  • 1
  • 6
  • 18
  • Try this http://stackoverflow.com/a/784611/2261259 – Voonic Sep 13 '13 at 06:55
  • @shadow: That's not what he wants. He wants the special character to be entered in the text field, in this case it's a non-breaking space. He doesn't want its HTML entity. – Joe Simmons Sep 13 '13 at 06:59
  • The code in the question does not have the effect described. Did you actually mean `"&nbsp"` instead of just `"nbsp"`? – Jukka K. Korpela Sep 13 '13 at 10:08

5 Answers5

4

It seems that you want to enter special characters like NO-BREAK SPACE in a JavaScript string literal. You can do that directly, provided that the character encoding of the file containing JavaScript code is properly declared, as it should be anyway:

document.getElementById("textField").value = ' ';

Here the character between apostrophes is the real NO-BREAK SPACE character. In rendering, it is usually indistinguishable from normal SPACE, but it has different effects. Similarly you can write e.g.

document.getElementById("textField").value = 'Ω';

using the Greek letter capital omega directly.

If you do not know how to enter such characters (e.g., via Windows CharMap program) or if you cannot control character encoding issues, you can use JavaScript Unicode escape notations for characters, e.g.

document.getElementById("textField").value = '\u00A0'; // no-break space

or

document.getElementById("textField").value = '\u03A9'; // capital omega

For the small set of characters with Unicode numbers less than 0x100, you can alternatively use \x escapes, e.g. '\xA0' instead of '\u00A0'. (But if you didn’t know this, it is better to learn to use the universal \u escape insteadd.)

Jukka K. Korpela
  • 195,524
  • 37
  • 270
  • 390
1

  is an HTML entity and you can't put an HTML entity in a text field like that.


Try using unicode, like this:

document.getElementById("textField").value = '\xA0';
Joe Simmons
  • 1,828
  • 2
  • 12
  • 9
0
document.getElementById("textField").value = " ";
Autocrab
  • 3,474
  • 1
  • 15
  • 15
0

you should use " " instead of "nbsp"

  • what about the angle sign what about the arrow how can I type those? &nbsp is only serving as a example here I am trying to illustrate the problem – user1935724 Sep 13 '13 at 16:22
  • & (ampersand) becomes & " (double quote) becomes " ' (single quote) becomes ' < (less than) becomes < > (greater than) becomes > you need to convert each characters to its specific HTML entry you can use simple php function htmlspecialchars() function to convert special characters to html entries. or you can use
     tag to display all characters written inside this tag.
    –  Sep 13 '13 at 17:47
  • would it be easier than just do $(element).html(encodedString).text()? – user1935724 Sep 13 '13 at 22:38
  • there are several ways to do a single things its depends on you which way you like –  Sep 14 '13 at 11:59
0

What about using jquery and this:

$("#textField").html('&nbsp').text()

Or in more general:

$(element).html(encodedString).text()
mhafellner
  • 458
  • 3
  • 9