Avatar of eNarc
eNarc
Flag for United Kingdom of Great Britain and Northern Ireland asked on

how to put html code not display in web page but as code itself

Hi, I'm wanting to put code on a webpage without affecting the web page layout though still displaying the actual code on the web page. is it possible to do this?
HTMLPHPJavaScript

Avatar of undefined
Last Comment
eNarc

8/22/2022 - Mon
experts1

Try example below:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 1</title>
<script language="javascript">
function myHTML()
{
  var ct = "<input type='button' value='Click' onclick='myHTML();'>";
  document.getElementById("mydiv").innerText = ct;
}
</script>
</head>
<body id="mybody">
<input type="button" value="Click" onclick="myHTML();">
<br>
<div id="mydiv">
</div>
</body>
</html>

Open in new window

eNarc

ASKER
that didn't work.

when I put this below it just showed the textarea.

<script language="javascript">
function myHTML()
{
  var ct = "<textarea><input type='button' value='Click' onclick='myHTML();'></script><textarea>";
  document.getElementById("mydiv").innerText = ct;
}
</script>
</head>
<body id="mybody">
<input type="button" value="Click" onclick="myHTML();">
<br>
<div id="mydiv">
</div>

Open in new window

experts1

You inserted a premature "</script>" in your javascript code
which cause error, premature end of script:

var ct = "<textarea><input type='button' value='Click' onclick='myHTML();'></script><textarea>";
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
eNarc

ASKER
though thats what I'm saying it doesn't just show the code it changes the layout.
Roopesh Reddy

Hi,

You may have to try the following code!

 <div>
        &lt;input type="text" /&gt;
    </div>

Open in new window


Hope it helps u...
hielo

If you are going to "embed" <script> OR </script> within the dynamic string, you will need to "break" the <script> into separate strings and then join them.  To clarify, if you want:
<script type='text/javascript'>  you will need to use "...<scr" + "ript type='text/javascript'>..."

Likewise:
</script>  you will need to use "...</scr" + "ript>..."

Otherwise the parser "thinks" that you are trying end the actual script block (instead of just appending some text the spells out "<script>".  Also, escape the < and the > characters (see attached code).

<script language="javascript">
function myHTML()
{
  var ct = "<textarea><input type='button' value='Click' onclick='myHTML();'><scr"+"ipt type='text/javascript'>...</"+"script><textarea>";
  document.getElementById("mydiv").innerHTML = ct.replace(/([<>])/g,function(m,c){return '&#'+c.charCodeAt(0)+';';});
}
</script>
</head>
<body id="mybody">
<input type="button" value="Click" onclick="myHTML();">
<br>
<div id="mydiv">
</div>

Open in new window

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Ashok

Try

document.getElementById("mydiv").value = ct;

HTH
Ashok
experts1

In other words, you have to escape some characters
as follows:

Note: "/" is escaped by using "\/",  and code should work!
var ct = "<textarea><input type='button' value='Click' onclick='myHTML();'>[b]<\/script>[/b]<textarea>"; 

Open in new window

ASKER CERTIFIED SOLUTION
eNarc

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
eNarc

ASKER
is the correct answer.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy