I have written a basic code in CodeMirror's textarea and now facing problem in executing the code written.
Below is the HTML part:
       <form id="preview-form" method="post" action="<?php echo 
        $_SERVER['PHP_SELF']; ?>">
        <textarea class="codemirror-textarea" name="preview-form-comment" 
        id="preview-form-comment">
            #include<stdio.h>
            int main()
            {
                printf("Hello World!!");
            }
        </textarea>
        <input type="test" name="testID" id="testID" value="aqsa shahid">
        <br>
        <input type="submit" name="preview-form-submit" id="preview-form-
        submit" value="Submit" onclick="test();">
        </form> 
        <div id="preview-comment"></div>
This is the function to execute code.
        function test() {
        //code goes here
        alert(document.getElementById("preview-form-comment").value);        
        var code = $(".codemirror-textarea")[0];
        var editor = CodeMirror.fromTextArea(code, {
        lineNumbers: true,
        matchBrackets: true,
        mode: "text/x-csrc",
        theme: "dracula"
        });
        CodeMirror.runMode(document.getElementById("preview-form-
        comment").value, "text/x-csrc", document.getElementById('preview-
        comment')); };
                I know you probably solved this and it's old, but i will put this in case someone else is looking for the answer
i solved this using an iframe then in js
iframe.src = "data:text/html;charset=utf-8," + encodeURI(new_textarea.value)
and it worked for me .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With