Html entities in javascript alert?

I have a line coming from XML (which I cannot edit) and I would like to print it through an alert in javascript.

An example of my line:

This is à string

And I need to type in the message:

This is à string

is there any js html decoding?

+5
source share
3 answers
var encoded = "This is à string";
var decoded = $("<div/>").html(encoded).text();
alert(decoded);
+12
source

you can put the string in the dom element and read it again, even without jquery: fooobar.com/questions/40529 / ...

Edit the recent requirement to include some code from another SO answer:

var div = document.createElement('div');
div.innerHTML = encoded;
var decoded = div.firstChild.nodeValue;
+8
source

I'm a little late, but just in case someone finds it via Google (like me), I thought I would improve on Imperative .

function showbullet() {
  var tempelement = document.createElement('div');
  tempelement.innerHTML = "&bull;";
  alert("Here, have a bullet!\n" + tempelement.innerHTML);
}
showbullet();

I tested this and confirmed that it works in Chrome / 43.0.2357.130 m; Firefox / 32.0.1; Internet Explorer / 9.0.8112.16421. There is no need to fuck with nodeValue and what not; the object will be replaced by an associated symbol as soon as the task is completed. (Note, however, that the execution alert(tempelement.innerHTML="&bull;");does not work in any of the browsers you tested!)

0
source

All Articles