1

I have a little problem here...

This is my code

<html>
 <textarea id="text">
  <h2>Text Here...</h2>
 </textarea>

 <div id="preview">

 </div>
</html>

<script>
  var syntax = document.getElementById("text").innerHTML;
  document.getElementById("preview").innerHTML = syntax;
</script>

The problem is the preview shows <h2>Text Here...</h2> instead of Text Here... with h2 font.

Thanks

Beginner Programmer

6
  • Use textContent instead. Commented Nov 22, 2016 at 11:19
  • 2
    with this code, preview actually shows [object HTMLDivElement] Commented Nov 22, 2016 at 11:21
  • Look at Azeez anwser. I have to retrive innerHtml from sintax. Commented Nov 22, 2016 at 11:22
  • @JaromandaX That's what happens with objects, heh (except functions (instanceof, though)) Commented Nov 22, 2016 at 11:24
  • @FREEZE - I know why it was that text, my point was that the question stated the displayed text in preview was completely different to that - clearly the code in the question is not exactly the code the OP was having issues with Commented Nov 22, 2016 at 11:26

1 Answer 1

4

Just change your script :

  var syntax = document.getElementById("text").innerHTML;
  document.getElementById("preview").innerHTML = syntax;
 <div id="text">
  <h2>Text Here...</h2>
 </div>

 <div id="preview">

 </div>

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

4 Comments

It was a 'div' in your question
I know, i have changed it
Then you need to change your code also: Please use this line: var syntax = document.getElementById("text").value;
Use .value instead of .innerHTML and try

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.