Set Value to CodeMirror textarea - Javascript CodeMirror

Javascript examples for CodeMirror:Configuration

Description

Set Value to CodeMirror textarea

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <title>Codemirror - Set Value</title> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.js"></script> 
      <script type="text/javascript" src="https://codemirror.net/lib/codemirror.js"></script> 
      <link rel="stylesheet" type="text/css" href="https://codemirror.net/lib/codemirror.css"> 
      <script type="text/javascript" src="https://codemirror.net/mode/xml/xml.js"></script> 
      <style id="compiled-css" type="text/css">

.CodeMirror {border-top: 1px solid black; border-bottom: 1px solid black;}


      </style> 
      <script type="text/javascript">
    $(window).load(function(){//from  w  ww  . java2 s. co m
$(document).ready(function() {
    var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
        mode: "text/html",
        lineNumbers: true,
    });
    $("#insert").click(function(){
       var content = $("#content").val();
       editor.setValue(content)
    });
});
    });

      </script> 
   </head> 
   <body> 
      <br>
      <br> 
      <input id="content" value="Type something "> 
      <br>
      <br> 
      <button id="insert">Insert Value</button> 
      <br>
      <br> 
      <textarea id="code" name="code">
&lt;?xml version="1.0"?&gt;
&lt;catalog&gt;
   &lt;book id="bk101"&gt;
      &lt;author&gt;Gambardella, Matthew&lt;/author&gt;
      &lt;title&gt;XML Developer's Guide&lt;/title&gt;
      &lt;genre&gt;Computer&lt;/genre&gt;
   &lt;/book&gt;
&lt;/catalog&gt;
</textarea>  
   </body>
</html>

Related Tutorials