Extract content from codemirror to use in a div

I’m using CodeMirror v2.36, because it had the autoformat option in it. What I’m doing is attempting to use jquery to pull the code out of the textarea that Codemirror is attached to and replace the html inside of a div with it.

Here is the small jQuery I’m using…

  $(document).ready(function(){
       $('#commit').click(function(){                          
            $('#output').html($('#html_code').val());
       });
 });

#output is the Div and #html_code is the textarea that CodeMirror is using. When I click the commit button the changes I made in codemirror aren’t moved over. What would I need to pull the code from CodeMirror and replace the HTML in #output with CodeMirrors code?

Thanks in advance for any help!

I finally was able to solve it. This is the code I ended up coming up with based on some stuff I read online!

 $(document).ready(function(){
      $('#commit').click(function(){   
           var myeditor = document.querySelector('.CodeMirror').CodeMirror;  
           $('#output').html(myeditor.getValue());
     });

});