Pedro Oliveira
Pedro Oliveira

Reputation: 145

Set Value of CodeMirror textarea from Jquery Ajax Call

I'm trying to get a value from a Ajax call in Jquery to a the value property of a Textarea that's using, CodeMirror scripts. I've tried the classic way to set the .html() and .val() properties of the textarea to my Ajax call data, parameter.

Here is my code :

<!--Replaces TextBoxes by the code oriented boxes-->
<script type="text/javascript">
  var editor = CodeMirror.fromTextArea(document.getElementById('code'),{
    mode: 'shell',
    lineNumbers: true,
    theme: 'blackboard'
  });
</script>
<!--Jquery Scripts for the Selects and file Edit-->
<script type="text/javascript">
  $(document).ready(function() {
    var directory = $("#directory").val();
    $.ajax({
      type: "POST",
      dataType: "text",
      url: "list_directory.php",
      data: "directory="+directory,
      cache: false,
      success: function(data){
        $("#files").html(data)
      },
      error: function(msg){
        $("#starterror").show()
      }
    });  
  });
  $("#directory").change(function(){
    var directory = $("#directory").val();
    //$("#aux_directory").val(file);
    //alert(file);
    $.ajax({
      type: "POST",
      dataType: "text",
      url: "list_directory.php",
      data: "directory="+directory,
      cache: false,
      success: function(data){
        $("#files").html(data)
      },
      error: function(msg){
        $("#starterror").show()
      }
    });
  })
  $(".edit_item").live('click',function(){
    var directory = $("#directory").val();
    var file = $(this).text();
    $.ajax({
      type: "POST",
      dataType: "text",
      url:"edit_file.php",
      data: { directory: directory, file: file},
      //data: "directory="+directory && "file="+file,
      cache: false,
      success: function(data){
        //alert(data);
        //var file_result = data;
        $("#code2").setValue(data);
      },
      error: function(msg){
        $("#error_loading_file").show()
      }
    });
 })

I'm running out of ideas!

Upvotes: 2

Views: 6718

Answers (1)

aljordan82
aljordan82

Reputation: 1720

Use editor.setValue()

See CodeMirror manual()

jsfiddle example

Upvotes: 3

Related Questions