Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript / CodeMirror - refresh textarea

How do I use the refresh function from CodeMirror 2?

refresh()

If your code does something to change the size of the editor element (window resizes are already listened for), or unhides it, you should probably follow up by calling this method to ensure CodeMirror is still looking as intended.

I want to refresh all textareas after a link is clicked

I tried

  $('.CodeMirror').each(function(){
    getElementById($(this).attr('id')).refresh();
  });

but it doesn't work....

like image 339
Alex Avatar asked Mar 19 '11 21:03

Alex


2 Answers

When you instantiate the CodeMirror instance, it is placed as a property on the wrapper div.

$('.CodeMirror').each(function(i, el){
    el.CodeMirror.refresh();
});

The above snippet does not recreate the editor, but instead uses the existing one.

like image 82
Jonathan Schneider Avatar answered Sep 28 '22 05:09

Jonathan Schneider


The refresh method (just like all other CodeMirror methods) does not live on the DOM node, but on the instance object that's returned when you create the editor (by calling CodeMirror or CodeMirror.fromTextArea). So you'll have to store those somewhere for this to work.

like image 38
Marijn Avatar answered Sep 28 '22 07:09

Marijn