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>
there are several wrong things in your code:
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>