RickyAYoder
RickyAYoder

Reputation: 991

Parse String as HTML in JavaScript

So, I have a textarea that users are supposed to type codes into.

Ex: <div id="example">blar</div>

How can I evaluate what they enter in the textarea as HTML (as in the DOM) without:

For example:

Code: <div id="ex">Blar</div>

My Function:

function parseCode(){
    var code = document.getElementById("code").value;
}

I want to take the value and make it accessible (like code.getElementsByTagName("div")[0].innerHTML);

Upvotes: 1

Views: 4354

Answers (1)

Chris
Chris

Reputation: 28064

var ele = document.createElement('div');
ele.innerHTML = document.getElementById('code').value;

This does not create a new element on the page itself, only in memory

Upvotes: 2

Related Questions