0

Here is my code where I try to build real-time html editor.In javascript I get the text from textarea which has id=pure then in document.body.onkeyup function I pass the value to the textarea that has id=compiled. It does not work at all. I wonder if the problem is about open-writeln-close or another syntax?

function compile() {

    var h = document.getElementById("pure");
    var compiled = document.getElementById("compiled").contentWindow.document;

     document.body.onkeyup = function(){
        compiled.open();
        compiled.writeln(h.value);
        compiled.close();
      };
    }

compile();




<div class="form-group">
  <label for="html">Write your HTML here:</label>
  <textarea class="form-control" rows="3" id="pure"></textarea><br>
    <textarea class="form-control" rows="2" id="compiled"></textarea>
</div>
1
  • I'm pretty sure you mean an <iframe> as compiled output. <textarea> doesnt have a contentWindow Commented Mar 21, 2018 at 14:56

2 Answers 2

1

Not 100% sure what your after looking at your code.

But if all your after is have a TextArea were you can put HTML markup, and then see a preview. Below is an example..

var h = document.getElementById("pure");
var compiled = document.getElementById("compiled");
h.onkeyup = function() {
  compiled.innerHTML = h.value;
  pure.classList.toggle("error",
    compiled.innerHTML !== h.value); 
};
h.onkeyup();
.error {
 background-color: red;
 color: white;
}
<label for="html">Write your HTML here:</label>
<br>
<textarea class="form-control" rows="3" id="pure">
Hello <b>world</b>
</textarea><br>
<div id="compiled"></div>

Sign up to request clarification or add additional context in comments.

2 Comments

Actualy this was what I was trying but I have a question. When you write <h1> Hallo</h2> or <h1> Hallo</h1> you get the same result. Is it possible to get the pure html on preview if it is not valid?
If you mean show you some sort of error, you just check the saved HTML and see if it's the same as your TextArea, if there different you know there is an Error. I've updated Snippet, type some invalid HTML and the TextArea will go red.
0
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body>

<div ng-app="">

<p>Input something in the input box:</p>
<p>Name: <input type="text" ng-model="name"></p>
<p ng-bind="name"></p>

</div>

</body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.