Question

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

Was it helpful?

Solution 3

What about using jquery and this:

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

Or in more general:

$(element).html(encodedString).text()

OTHER TIPS

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.)

  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';
document.getElementById("textField").value = " ";

you should use " " instead of "nbsp"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top