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,10 +1,8 @@
-function setAceEditor() {
+function set_ace_editor() {
   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() {    
@@ -14,19 +12,7 @@
   });
 };
 
-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()
+  set_ace_editor();
 });
Change editor language when another language is selected
