0

I managed to create a HTML/CSS editor, at http://hexagonest.tk/code/, but I can't manage it to work Javascript. I'm not sure why, all the code is valid. For example, go on this jfiddle, and type in this code:

<script>
function dothis(){
 document.getElementById("thewow").innerHTML = "poop!";
}
</script>
<button type="button" onclick="dothis()">Hi</button>
<p id="thewow">Hi</p>

In theory, it should work, yes? Even when I right click>inspect element, it shows that there is a valid tag. What's wrong with this?

If you didn't catch it, my problem is that Javascript is now working with my live HTML editor.

hexagonest
  • 612
  • 1
  • 10
  • 25
  • If you want all in one frame, refer to this accepted answer. http://stackoverflow.com/questions/5678787/w3school-try-it-editor. It sends the user input to your server side. You can then display this page in an iframe back on your editor. But make sure you know the security issues. – clouddreams Feb 04 '14 at 02:32

2 Answers2

0

You should use the eval() function from JavaScript.

more documentation here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval

Then you should have one form for the JavaScript and another for the html.

Beware about security issues that you can have with eval() (XSS vulnerabilities, etc...).

Yves Lange
  • 3,914
  • 3
  • 21
  • 33
0

try this

<button type="button" onclick="document.getElementById('thewow').innerHTML = 'poop!'">try me</button>
Hi

---addtional---

use this script to play with your code

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR...nsitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<style type="text/css">
textarea, iframe {width:200px;height:200px;}
td {vertical-align:top;text-align:center;}
</style>

<script type="text/javascript">
function tryit()
{
var html = document.getElementById("code").value;

ifrm = document.getElementById("output");
var doc = ifrm.contentDocument || ifrm.contentWindow.document;
doc.open();
doc.write(html);
doc.close();
}
</script>
</head>

<body>

<table cellspacing="4" cellpadding="0" border="0">
<tr>
<td>
<textarea id="code"><html><body>Hello World!</body></html></textarea>
<br />
<button onclick="tryit();">Do it!</button>
</td>
<td>
<iframe id="output"></iframe>
</td>
</tr>
</table>

</body>
</html>

source http://w3schools.invisionzone.com/index.php?showtopic=23599

Ansyori
  • 2,807
  • 3
  • 29
  • 37