0

I want to decode the chinese characters into the input text field.But it is showing the field as it is.

But it is showing the "&#28450 ;&#23383 ;" instead of chinese characters

Expected output :漢字
output :&#28450 ;&#23383 ;

it is working fine when i use textarea

input type="text" id="chinese"

function myFunction() {

var uri_dec = decodeURIComponent("漢字")
document.getElementById("chinese").value= uri_dec;

}

Please help me on this

Thanks in advance

Ananth
  • 51
  • 1
  • 9

1 Answers1

0

decodeURIComponent doesn't do what you think it does. It will decode "%E6%BC%A2%E5%AD%97" (an URI-encoded string) into "漢字"; but you have HTML entities, not an URI-encoded string.

var ent_enc = "漢字"
var div = document.createElement('div');
div.innerHTML = ent_enc;
var ent_dec = div.textContent;
document.getElementById("chinese").value = ent_dec;
<input type="text" id="chinese">
Amadan
  • 191,408
  • 23
  • 240
  • 301
  • The `innerHTML` usage is unsafe if your input could be untrusted. For example, `var ent_enc = ""` leads to code execution. Using `document.createElement('textarea')` would be safer, as it can't have any child nodes beyond plain text. – ephemient Feb 15 '17 at 07:01