How to Cache DOM Variable in jQuery

How to Cache DOM Variable in jQuery


Using jquery and highchart.js (+ exporting.js module) I am trying to export the chart by a custom button like below

  1. <button type="button" id="chartpdf" data-chart="chart1" class="pdf-download">PDF</button>
While the code is exporting the chart in hard-coded format like this

  1. chart1.exportChart({type: "application/pdf"});
it is not working when trying to dynamically get the chart1 from the element data attribute data-chart="chart1"

  1. $(document).on("click", ".pdf-download", function(){
  2.              $(this).data('chart').exportChart({type: "application/pdf"});
  3. });

can you please let me know how to fix this?