function setAceEditor() {
  var textarea = document.getElementById("editor");
  var form = textarea.form

  editor = ace.edit(textarea)
  language = $(":selected").html()
  changeEditorLanguage(language)
  editor.container.id = "ta"

  form.addEventListener("submit", function() {    
    textarea.style.visibility = "hidden"
    textarea.value = editor.getValue()
    form.appendChild(textarea)
  });
};

function onSelectChange() {
  $("#exercise_language_id").change(function() {
    language = $(":selected").html()
    changeEditorLanguage(language)
  });
};

function changeEditorLanguage(language) {
  editor.getSession().setMode("ace/mode/"+language.toLowerCase())
};


$(document).ready(function() {
  setAceEditor()
  onSelectChange()
});
function set_ace_editor() {
  var textarea = document.getElementById("editor");
  var form = textarea.form

  editor = ace.edit(textarea)
  editor.container.id = "ta"

  form.addEventListener("submit", function() {    
    textarea.style.visibility = "hidden"
    textarea.value = editor.getValue()
    form.appendChild(textarea)
  });
};


$(document).ready(function() {
  set_ace_editor();
});
--- 
+++ 
@@ -1,8 +1,10 @@
-function set_ace_editor() {
+function setAceEditor() {
   var textarea = document.getElementById("editor");
   var form = textarea.form
 
   editor = ace.edit(textarea)
+  language = $(":selected").html()
+  changeEditorLanguage(language)
   editor.container.id = "ta"
 
   form.addEventListener("submit", function() {    
@@ -12,7 +14,19 @@
   });
 };
 
+function onSelectChange() {
+  $("#exercise_language_id").change(function() {
+    language = $(":selected").html()
+    changeEditorLanguage(language)
+  });
+};
+
+function changeEditorLanguage(language) {
+  editor.getSession().setMode("ace/mode/"+language.toLowerCase())
+};
+
 
 $(document).ready(function() {
-  set_ace_editor();
+  setAceEditor()
+  onSelectChange()
 });
Change editor language when another language is selected
