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

221
Visualizações
Javascript adds another div instead of overwriting

I am trying to change the chart content with the following script:

function switchType() {
    var e = document.getElementById("typeSelect");
   var selected_value = e.options[e.selectedIndex].value;
   console.log(selected_value)

   anychart.onDocumentReady(function () {
     var ents = {{ entities_dict|safe }}
     var dropdown = document.getElementById("typeSelect");
     var val = dropdown.value;
     /*
     var data = [
       {value: 'GeopoliticalEntity',
        children: [
        {% for i in entities_dict.get('GeopoliticalEntity') %}
           {value:   "{{ i }}"},
         {% endfor %}

       ]}
     ];
     */
     var data = [
     ];

     data.push({value:     val,
            children: [
               {value:   "country"},
               {value:   "europe"},
               {value:   "German"},

           ]})
     //let result = data.map(a => a.children);

     // create a chart and set the data
     var chart = anychart.wordtree(data, "as-tree");

     // set the chart title
     chart.title("Word Tree: Data (Tree)");

     // set the container id
     chart.container("word_tree");

     // initiate drawing the chart
     chart.draw();

 });
  }

This is my HTML

<div class="container">
  <div class="jumbotron">
    {% for k in survey %}
      <h2 class="display-4">Keyword Analysis</h2>
      <h2 class="display-5">Most frequent keywords</h2>
      <div id="word_cloud" style="width: 1000px; height: 500px; margin-bottom:5px;"></div><br>
      <h2 class="display-5">Keyword frequency and their relevance score</h2>
      <div id="bubble_chart" style="width: 1000px; height: 500px;"></div><br>
      <h2 class="display-5">Entities found in responses</h2>
      <select id="typeSelect" onchange="switchType()">
        {% for key in entities_dict.keys() %}
          {% if loop.first %}
            <option selected="selected" value="{{ key }}">{{ key }}</option>
          {% else %}
            <option selected="selected" value="{{ key }}">{{ key }}</option>
          {% endif %}
        {% endfor %}
    </select>
      <div id="word_tree" style="width: 1000px; height: 500px;"></div>
    {% endfor %}
  </div>
</div>

The result is that it creates the chart, then creates another one below, and another one on each select change.

Is there any way to overwrite the requested div instead of appending another one to the HTML page?

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

0

This piece of code I found on this thread solved it for me: How to overwrite html element from javascript?

     if (element) {
    // Get its parent
        parent = element.parentNode;

        // Create the new element
        newElement = document.createElement('div');

        // Set its ID and content
        newElement.id = "word_tree";

        // Insert the new one in front of the old one (this temporarily
        // creates an invalid DOM tree [two elements with the same ID],
        // but that's harmless because we're about to fix that).
        parent.insertBefore(newElement, element);

        // Remove the original
        parent.removeChild(element);
    }
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