Appending symbol code to HTML from a JS function

I am trying to append a html symbol code – ☀ which is a sun to an html span.innerHTML using a js function but what I get in is

<span class="condition symbol">&amp;#x2600;</span>

And in this way it shows ☀ instead of the sun symbol.

I tried using “, ”,"" while appending the symbol’s code but same.

const htmlElement=document.createElement('span');
... //adding the classes
htmlElement.textContent='&#x2600;';

>Solution :

Use innerHTML instead of textContent to render the symbol.

const htmlElement=document.createElement('span');
//adding the classes
htmlElement.innerHTML='&#x2600;';
document.body.append(htmlElement);

Leave a Reply