How to decode a string in a variable in javascript?

I have code in javascript:

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

Now I want to decode the location of the text in

"HCM - NYC (New York, NY)"

I ask for advice. Thank.

+3
source share
2 answers

JavaScript does not have a specific function that will decode HTML objects, however you can assign a property innerHTMLto an element and then read it.

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

This will work for any HTML objects, not just <

change

As indicated below, you are halfway through, you are using the wrong property.

Edit:

td_Details.innerText = location;

at

td_Details.innerHTML = location;

In the future, innerHTMLavailable in all browsers. innerTextno.

+3
source

To remove "just use the following:

location = location.replace(/&quot;/g, '');

, . :

location = location.replace(/&quot;/g, '"');
+2

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


All Articles