Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

354
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!