Search code examples
javascriptphpjqueryajaxcodemirror

How to get CodeMirror value without defining variable?


I'm using multiple CodeMirror areas on my page and im defining them like this in js:

function editor(id){
  CodeMirror.fromTextArea(document.getElementById(id), {
    lineNumbers: false,
    styleActiveLine: true,
    matchBrackets: true
  });
}

And later I call this function like this on my textareas -

$j = 0;
while($row = mysql_fetch_array($formulas))
{
  echo "
  <textarea id='code".$j."'>;some code</textarea>
  --not important code--
  <script>editor('code".$j."');</script>";
  $j++;
}

and later, according to some solution from this site I'm refreshing part of my CodeMirrors because of placing them in hidden div like this

setTimeout(function() {
  $('.my-hidden-div .CodeMirror').each(function(i, el){
      el.CodeMirror.refresh();
  });

Now I need to get values of my CodeMirrors blocks by clicking in button in same row and send them via AJAX but problem appears.

How can I get value of them by using $(this).closest('tr') guys? Its important for me to not define my CodeMirror as a variable because I'm already supporting dynamic deleting and adding new CodeMirrors textareas. But anyway if I did it, I was getting an error that js cannot recognize my variable, that was defined in other js function and it's not global. Help me guys and have a nice day. Greetings.


Solution

  • The same thing that you're already using to call refresh, accessing the CodeMirror property on the DOM node, can be used to call getValue as well.