skip to Main Content

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='☀';

2

Answers


  1. Use innerHTML instead of textContent to render the symbol.

    const htmlElement=document.createElement('span');
    //adding the classes
    htmlElement.innerHTML='&#x2600;';
    document.body.append(htmlElement);
    Login or Signup to reply.
  2. As mentioned, you can use innerHTML to display the symbol.
    Or you can also use textContent and set the hexadecimal content like this

        const htmlElement = document.createElement('span');
        htmlElement.textContent = "u2600";
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search