Decoding the chinese characters in input text field

1.2k Views Asked by At

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

1

There are 1 best solutions below

1
On

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">