Special html characters are displayed as characters

I tried to dynamically set the contents of text input using JS, the problem I encountered was that the browser cannot display special characters, not characters, for example

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

Instead of displaying the space it displays & nbsp, did anyone get any idea?

thanks a lot

+4
source share
5 answers

How about using jquery and this:

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

Or more generally:

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

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

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

Here, the symbol between the apostrophes is the true symbol of NO-BREAK SPACE. In rendering, it is usually indistinguishable from the usual SPACE, but it has different effects. Similarly, you can write, for example.

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

using the direct link to the Greek letter omega.

If you do not know how to enter such characters (for example, through the Windows CharMap program) or if you cannot manage character encoding problems, you can use JavaScript Unicode escape notifications for characters, for example

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

or

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

For a small character set with Unicode numbers less than 0x100, you can use \x escape sequences, for example. '\xA0' instead of '\u00A0' . (But if you didn’t know this, it’s best to learn how to use the universal \u escape instead.)

+2
source

  is an HTML object, and you cannot put an HTML object in a text box like this.


Try using unicode, for example:

 document.getElementById("textField").value = '\xA0'; 
+1
source
 document.getElementById("textField").value = " "; 
0
source

you should use "" instead of "nbsp"

0
source

Source: https://habr.com/ru/post/1501998/


All Articles