Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
iam trying to create a code editor ,i have used code mirror for syntax hilghlighting and is linked in text area now the problem iam facing is iam unable to pass the text area data to the compiler function so that html,css and js get compiles and output get displayed in iframe.

i dont know how to pass the value from codemirror to compiler

here are codes

code mirror:
JavaScript
var htmleditor = CodeMirror.fromTextArea
   (document.getElementById('html'), {
    mode:"xml",
    theme:"dracula",
    lineNumbers:true
  });




compiler function:
JavaScript
function compile() {
  var html = document.getElementById("html");
  var css = document.getElementById("css");
  var js = document.getElementById("js");
  var code = document.getElementById("code").contentWindow.document;

  document.body.onkeyup = function() {
    code.open();
    code.writeln(
      html.value +
        "<style>" +
        css.value +
        "</style>" +
        "<script>" +
        js.value +
        "</script>"
    );
    code.close();
  };
}

compile();



please suggest a valid solution


What I have tried:

i have tried passing value by getelementbyid but doesnt seem to work for me
Posted
Updated 15-May-22 22:37pm

1 solution

Looking at the documentation[^], the underlying element's value will be updated when the form is submitted.

If you want to get the current value, you need to call the save method first.
JavaScript
document.body.onkeyup = function(){
    htmleditor.save();
    console.debug(html.value);
    ...
};
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900