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

356
Visualizações
changing the data of a variable on button clicks

Im trying to change the array inside of a variable by different button click, but im unable to do it. Is there any way i can do this.

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js">
  var chartdata = [];
  var eqdata = [['a',2],['b',6],['r',9],['s',8]];
  var debdata =  [['p',2],['r',6],['r',9],['d',8]];
  var hybdata =  [['l',2],['k',6],['rr',9],['df',8]];
  
  function clicked(){
    var id = event.srcElement.id
    if (id = 'eqty'){
        return chartdata = eqdata;
    } else if (id = "debt"){
        return chartdata = debdata;
    } else if (id = "hyb"){
        return chartdata = hybdata;
    }
};
</script>
<div class="row" style="margin:auto; padding: 8px 8px 0" id="categories">
  <button id="eqty" class="btn btn-primary" style='margin: 0 8px' onclick="clicked()">Equity Chart</button>
  <button id="debt" class="btn btn-primary" style='margin: 0 8px' onclick="clicked()">Debt Chart</button>
  <button id="hyb" class="btn btn-primary" style='margin: 0 8px' onclick="clicked()">Hybrid Chart</button>
</div>

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

0

there are several wrong things in your code:

  1. you shouldn't write your script in an import script (line 1)
  2. you should use es6's let and const instead of var
  3. in your checks (lines 9, 11 and 13) you should have at least a double equal sign == to compare, one equal sign is used to assign values.

Fixing these little things you should end up with something like this:

<div class="row" style="margin:auto; padding: 8px 8px 0" id="categories">
<button id="eqty" class="btn btn-primary" style='margin: 0 8px' onclick="clicked()">Equity Chart</button>
<button id="debt" class="btn btn-primary" style='margin: 0 8px' onclick="clicked()">Debt Chart</button>
<button id="hyb" class="btn btn-primary" style='margin: 0 8px' onclick="clicked()">Hybrid Chart</button>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
    let chartdata = [];
    const eqdata = [['a',2],['b',6],['r',9],['s',8]];
    const debdata =  [['p',2],['r',6],['r',9],['d',8]];
    const hybdata =  [['l',2],['k',6],['rr',9],['df',8]];
    
    function clicked(){
        const id = event.srcElement.id
        if (id == 'eqty'){
            chartdata = eqdata;
        } else if (id == "debt"){
            chartdata = debdata;
        } else if (id == "hyb"){
            chartdata = hybdata;
        }
    };
</script>
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