Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to get the createTextNode method to render html tags?

Tags:

javascript

The following code prints

This should print(b)This should print(/b)This should print

<script>
function produceMessage(){
    var msg= '<b>This should print</b>';
    return msg;
}

</script>

<span id="mySpan"></span>

<script>

    document.body.appendChild(document.createTextNode(produceMessage()));
    document.write(produceMessage());
    document.getElementById('mySpan').innerHTML=produceMessage();
</script>
like image 308
user784637 Avatar asked Aug 06 '11 07:08

user784637


1 Answers

No, a text node will not print any HTML. Instead, create an element, or use a document fragment to insert HTML in that way.

function boldHTML() {
  var element = document.createElement("b");
  element.innerHTML = "Bold text";
  return element;
}
document.body.appendChild(boldHTML());

will print Bold text.

like image 200
Digital Plane Avatar answered Nov 07 '22 07:11

Digital Plane