jquery - Display javascript code with text() -


i'm trying display javascript within pre/code tags using jquery text() method. this:

 jquery(".output").wrapinner( "<pre><code></code></pre>" );  jquery(".output code").text("<script type='text/javascript' src='http://somedomain/?" + apikeyvalue.val() + "></script>"); 

however script tags produce

uncaught syntaxerror: unexpected token illegal

error in console. how can display script tags?

replace

jquery(".output code").text("<script type='text/javascript' src='http://somedomain/?" + apikeyvalue.val() + "></script>"); 

with:

jquery(".output code").text("<script type='text/javascript' src='http://somedomain/?" + apikeyvalue.val() + "></"+"script>"); 

or:

jquery(".output code").text("<script type='text/javascript' src='http://somedomain/?" + apikeyvalue.val() + "><\/script>"); 

you need break </script> tag ("</"+"script>") or escape / ("<\/script>")

as pointed out treetree in comment, detailed explanation, please read why split <script> tag when writing document.write()?


Comments