Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to decode a string in a variable in javascript?

Tags:

javascript

I have code in javascript:

var location = '"HCM - NYC (New York, NY)"';
td_Details.innerText = location;

Now I want to decode the text location to

"HCM - NYC (New York, NY)"

Please advice. Thanks.

like image 415
jeff Avatar asked Sep 14 '26 09:09

jeff


1 Answers

There is no specific function in JavaScript which will decode HTML entities, however you can assign an innerHTML property to an element and then read it back.

x = document.createElement('div');
x.innerHTML = ""test"";
console.log(x.innerHTML); // => "test"

This will work for any HTML entities, not just "

edit:

As pointed out below, you're half-way there, you're just using the wrong property.

Change:

td_Details.innerText = location;

to:

td_Details.innerHTML = location;

For future reference, innerHTML is available in all browsers. innerText is not.

like image 92
Matt Avatar answered Sep 16 '26 22:09

Matt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!