Search code examples
javascriptarrayshandsontable

How to transfer handsontable data to a different JavaScript on the same page?


Here is the link to jsfiddle code of handsontable. As the button is pressed, the data set goes to console. I have two different pieces of JavaScript code on my webpage. One of them contains handsontable. How can i transfer (dump) the data to an array belonging to a different piece of script (instead of console)? Is there any way to create a static array seen to ALL pieces of scripts (like in Java)? Here is the scheme:

<div id="mytable" class="handsontable"> </div>
<script>
// Here the user inserts the data to the handsontable. As she presses the button, data should go to the second piece of script.
</script>

...

<div id="target" class=""> </div>
<script>
//the target array is here, and it needs to be filled with the data from the above piece of script as the button is pressed.
</script>

Solution

  • There are many way to do it for example:

    1.) create global variable to hold the data

    <div id="mytable" class="handsontable"> </div>
    
    <script>
     // set data
      window.data = getCarData(); 
    
      $("#example1").handsontable({
         data:  window.data
      });
    </script>
    
    <div id="target" class="class"> </div>
    
    <script>
      //get data 
      var array = window.data;
    </script>
    

    2.) If you use jQuery:

    // you can bind data to element
    $('table').data('key', 'value');
    
    // and then get it
    $('table').data('key');