InnerHTML with special character is trimming the data

3.4k views Asked by At

InnerHTML with special character is trimming the data.

elem.innerHTML = displayedObjects.name;

here the displayedObjects.name contains a string like Test&string. The above statement is assigning the value only Test,

What could be done here?

1

There are 1 answers

0
p.s.w.g On

That's because Test&string isn't actually valid HTML, because & is an escape character for an HTML entity. If it were properly encoded, it would be Test&string instead.

If you're just trying to set the text of an element, I'd suggest you use innerText instead:

elem.innerText = displayedObjects.name;