Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

156
Visualizações
Multiple text-areas being added after an update event

I am making a simple code editor using codemirror. I am at the last stage but a bug has occurred which I am not able to get my heads around.

I have a select html tag with three options:

<select class="language" id="lang" onclick="update()">
  <option value="javascript">JavaScript</option>
  <option value="python">Python</option>
  <option value="python">C</option>
</select>

And my update() function is:

function update() {
  var select = $('#lang').val();
  var editor = CodeMirror.fromTextArea($('#text'), {
      mode: select,
      theme: "blackboard",
      });
}
update();

Now what happens is that everytime I select a different option from the dropdown, a new Code editor is added below the previous one.

Do you have some idea where I am going wrong in the code and how I can workaround?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Change your JS to this:

let editor;
function update() {
  var select = $('#lang').val();
  editor = editor ?? CodeMirror.fromTextArea($('#text'), {
      theme: "blackboard",
  });
  editor.setOption('mode', select);
}
update();

This makes it so if editor already exists, it is not created again, but instead re-used.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda