-1

I have created a span element using:

ele= document.createElement(span);
ele.innerText = "bla bla (<a href='https://www.pepe.com'> ble ble </a>) blu blu";
divele.appendChild(ele); #divele is an div in the page

The problem is the tag is not showed as a link, it is showed literally.

  • 3
    Use `ele.innerHTML` and remove the parenthesis around the anchor – Matt L. Jul 16 '18 at 14:53
  • Possible duplicate of [innerText property is not encoding the html](https://stackoverflow.com/questions/30426903/innertext-property-is-not-encoding-the-html) – Enzo B. Jul 16 '18 at 14:54

1 Answers1

3

Should be innerHTML instead of innerText (since the sting contains HTML in it). The span should be enclosed with single quotes ('') or double quotes ("").

var ele= document.createElement('span');
ele.innerHTML = "bla bla (<a href='https://www.pepe.com'> ble ble </a>) blu blu";
var divele = document.getElementById('divele');
divele.appendChild(ele);
<div id="divele"><div>
Mamun
  • 66,969
  • 9
  • 47
  • 59