Ferenc Schenkovski
Ferenc Schenkovski

Reputation: 89

Html Code inside an <pre> tag, innerHTML doesnt return all the code

I want all the code inside an pre-tag but innerHTML or jquery : html() doesnt return all the code. Some things where cut off. Like doctype und head body tags etc.

<pre id="pre">
    <!DOCTYPE html>
    <html >
        <head></head>
        <body>    
            <div>asdfds</div>
        </body>    
    </html>
</pre>

When i alert the innerHTML of the #pre object it only alerts the div

http://jsfiddle.net/6o8xjtaq/1/

Is there any other possibility to solve this?

Upvotes: 1

Views: 1164

Answers (2)

cforcloud
cforcloud

Reputation: 589

If you really want to do it, use a script

<script id="pre" type="text/template">
<!DOCTYPE html>
<html >
    <head></head>
    <body>    
        <div>asdfds</div>
    </body>    
</html>
</script>

$('#pre').text();

Upvotes: 2

Amit Joki
Amit Joki

Reputation: 59232

Replace < and > with &lt; and &gt; so you can get it as wanted, yet not invalid html.

<pre id="pre">
&lt;!DOCTYPE html&gt;
    &lt;html &gt;
        &lt;head&gt;&lt;/head&gt;
        &lt;body&gt;    
            &lt;div&gt;asdfds&lt;/div&gt;
        &lt;/body&gt;    
    &lt;/html&gt;
</pre>

Upvotes: 2

Related Questions